什么是简单的 jquery 方法来在选中复选框时选择无线电,然后在取消选中复选框时取消选择无线电?

StackOverflow https://stackoverflow.com/questions/1269349

<input id="myCheckbox" type="checkbox" /> Please, please check me
    <input id="myRadio" type="radio" /> Am I selected ?

我想要以下行为:

  1. 当 myCheckbox 被选中时, myRadio 将被选中。
  2. 当 myCheckbox 未被选中时, myRadio 将被取消选中。

我将如何以一种非常简单的方式做到这一点?

预先感谢您的努力回复。

有帮助吗?

解决方案

$('#myCheckbox').click(function() {
    if ($(this).is(':checked')) {
        $('#myRadio').attr('checked', 'checked');
    } else {
        $('#myRadio').removeAttr('checked');
    }
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top