繁体   English   中英

复选框更改事件上选中的单选按钮

[英]checked radiobutton on checkbox change event

我们如何选中或取消选中复选框上的所有单选按钮。如果复选框被选中,则所有单选按钮也被选中,反之亦然..它不能正常工作

<input type="checkbox" id="Check" />SelectAll<br /><input type="radio"/>First<br />


    <input type="radio"/>Second<br />
            <input type="radio"/>Third<br />
            <input type="radio"/>Fourth<br />
            <input type="radio"/>Fifth</div>
            <script>
                var IsCheck = false;
                $(document).ready(function () {
                    $("#Check").change(function () {
                        if (IsCheck == false) {
                            $("input[type=radio]").attr("checked", true);
                            IsCheck == true
                        }
                        else { $("input[type=radio]").attr("checked", false); IsCheck == false }
                    });
                }); </script>

请注意,您只是比较操作数,而不是在此语句中分配给变量:

IsCheck == true  
         ^------ REMOVE ONE = so it's an assignment

另外,请勿使用.attr("checked", true); 正确的形式是:

$("input[type=radio]").attr("checked", 'checked'); //checking for jQuery < 1.6

并取消选中:

$("input[type=radio]").removeAttr("checked");  //unchecking for jQuery < 1.6

如果您使用的是jQuery> 1.6,则可以将.prop()方法与布尔值一起使用,这与您尝试使用它的方式类似:

$("input[type=radio]").prop("checked", true); //checking for jQuery >= 1.6
$("input[type=radio]").prop("checked", false); //unchecking for jQuery >= 1.6

适用于jQuery 1.9或更高版本

$('input[type=radio]').prop("checked", true)

否则尝试

 $('input[type=radio]').attr('checked', 'checked');

尝试这个

 $(document).ready(function () {                    
    $("#Check").change(function () {                        
      $("input[type=radio]").attr("checked", $("#Check").is(":checked"));                            
    });
 });

演示版

对于您的问题,这可能是答案:

$("#Check").change(function () {
    $("input:radio").prop("checked", this.checked);
});

演示: http : //jsfiddle.net/hungerpain/QSx29/

也就是说,单选按钮不是执行此操作的最佳方法。 语义上不正确。 您只能在一个组中选择一个单选按钮。 尝试改用复选框。 尝试更改您对此的标记:

<input type="checkbox" id="Check" />SelectAll
<br />
<input type="checkbox" />First
<br />
<input type="checkbox" />Second
<br />
<input type="checkbox" />Third
<br />
<input type="checkbox" />Fourth
<br />
<input type="checkbox" />Fifth</div>

并将JS代码替换为:

$("#Check").change(function () {
    $("input:checkbox").prop("checked", this.checked);
});

这是一个演示: http : //jsfiddle.net/hungerpain/QSx29/1/

你只需要这个

$("#Check").change(function () {
    $("input[type=radio]").prop("checked", this.checked);
});

演示----> http://jsfiddle.net/kLnyD/

尝试这个

http://jsfiddle.net/4u9sQ/

 $("#Check").change(function () {
                        if ($(this).is(':checked')) {
                            $("input[type=radio]").prop("checked", true);

                        }
                        else { $("input[type=radio]").prop("checked", false); }
                    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM