我有一个结帐页面,其中有两个单选按钮,一个用于“注册帐户”和“访客帐户”结帐方法。
我想要一个复选框,当选中它时,它会检查注册帐户单选按钮,当未选中它时,它会检查 guest 帐户结帐单选按钮.
这是到目前为止我的代码: http://jsfiddle.net/hQbpZ/160/
HTML:
Remember Me for Future Purposes :<input type="checkbox" id="checkbox1"/> <br/><br/>
Register Account :<input type="radio" id="radio1" name="radio"/><br>
Guest Checkout :<input type="radio" id="radio2" name="radio"/><br>
JS:
jQuery('#checkbox1').click(function(){
jQuery('#radio1').attr('checked', true);
});
jQuery('#checkbox1').click(function(){
jQuery('#radio2').attr('checked', false);
});
我关闭了部分功能,但我不知道如何在取消选中复选框时取消选中单选按钮。
最佳答案
你可以这样做:
jQuery('#checkbox1').click(function () {
jQuery('#radio1').prop('checked', $(this).is(':checked'));
jQuery('#radio2').prop('checked', !$(this).is(':checked'));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Remember Me for Future Purposes :
<input type="checkbox" id="checkbox1" />
<br/>
<br/>Register Account :
<input type="radio" id="radio1" name="radio" />
<br>Guest Checkout :
<input type="radio" id="radio2" name="radio" />
<br>
关于javascript - 复选框控制单选选中/未选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31522803/