简体   繁体   English

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

[英]checked radiobutton on checkbox change event

how we checked or unchecked all radiobutton on checkbox click.. if checkbox checked then all radiobutton also checked and vice versa.. it is not working properly 我们如何选中或取消选中复选框上的所有单选按钮。如果复选框被选中,则所有单选按钮也被选中,反之亦然..它不能正常工作

<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>

Take care you were just comparing operands instead of assigning to a variable in this statements: 请注意,您只是比较操作数,而不是在此语句中分配给变量:

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

Also, don't use .attr("checked", true); 另外,请勿使用.attr("checked", true); the correct form is: 正确的形式是:

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

And unchecking: 并取消选中:

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

If you are using jQuery > 1.6 you can use the .prop() method with a boolean, which is similar to how you were trying to use it: 如果您使用的是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

For jQuery 1.9 or higher use 适用于jQuery 1.9或更高版本

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

Otherwise try 否则尝试

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

Try this 尝试这个

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

Demo 演示版

For your question, this could be the answer : 对于您的问题,这可能是答案:

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

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

That said, radio buttons are not the best way of doing this. 也就是说,单选按钮不是执行此操作的最佳方法。 Its not semantically right. 语义上不正确。 You can have only one radio button selected in a group. 您只能在一个组中选择一个单选按钮。 Try using checkboxes instead. 尝试改用复选框。 Try to change you're markup to this : 尝试更改您对此的标记:

<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>

And replace the JS code to this : 并将JS代码替换为:

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

Here's a demo : http://jsfiddle.net/hungerpain/QSx29/1/ 这是一个演示: http : //jsfiddle.net/hungerpain/QSx29/1/

You just need this 你只需要这个

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

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

try this 尝试这个

http://jsfiddle.net/4u9sQ/ 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