2013-06-22 115 views
-1

我们如何选中或取消选中复选框上点击所有单选按钮。如果选中复选框,那么所有单选按钮也查了,反之亦然..它不能正常检查单选按钮上的复选框更改事件

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

我不认为广播是正确的选择HTTP:/ /en.wikipedia。org/wiki/Radio_button通常只选择一个组中的一个选项 – HMR

+1

请不要对此使用单选按钮 – Jonathan

+2

(重要)注意:'var IsCheck = false;'引入一个全局变量,[这是一件坏事]( http://stackoverflow.com/questions/10525582/why-are-global-variables-considered-bad-practice-javascript)。将你的JavaScript代码封装在[自我调用函数]中(http://stackoverflow.com/questions/15313163/javascript-self-invoking-function)。 –

回答

1

工作试试这个

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

Demo

+0

谢谢你...它的工作... –

3

照顾你只是比较操作数,而不是在这个语句赋值给一个变量:

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 
1

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

$("#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/

1

你只需要这个

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

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

+0

-1使用单选按钮作为复选框 – Jonathan

+0

$(“input [type = radio]”)+1最有效的代码和第一次使用道具 – HMR

+0

是的,我会撤销-1为最佳代码... – Jonathan

2

jQuery的1.9或更高的使用

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

否则尽量

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

试试这个

http://jsfiddle.net/4u9sQ/

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

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