选中复选框时选择收音机然后取消选中复选框时取消选择收音机的简单jquery方法是啥?

Posted

技术标签:

【中文标题】选中复选框时选择收音机然后取消选中复选框时取消选择收音机的简单jquery方法是啥?【英文标题】:What is a simple jquery way to select a radio when checkbox is checked and then deselect the radio when checkbox unchecked?选中复选框时选择收音机然后取消选中复选框时取消选择收音机的简单jquery方法是什么? 【发布时间】:2010-11-19 03:41:28 【问题描述】:
<input id="myCheckbox" type="checkbox" /> Please, please check me
    <input id="myRadio" type="radio" /> Am I selected ?

我想要以下行为:

    当 myCheckbox 被选中时,myRadio 将被选中。 当 myCheckbox 未选中时,myRadio 将变为未选中状态。

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

提前感谢您的回复。

【问题讨论】:

【参考方案1】:
$('#myCheckbox').click(function() 
    if ($(this).is(':checked')) 
        $('#myRadio').attr('checked', 'checked');
     else 
        $('#myRadio').removeAttr('checked');
    
);

【讨论】:

哇。那很快。谢谢!

以上是关于选中复选框时选择收音机然后取消选中复选框时取消选择收音机的简单jquery方法是啥?的主要内容,如果未能解决你的问题,请参考以下文章