使用jQuery切换检查/取消选中多个复选框列表

Use*_*008 1 checkbox jquery toggle

检查和取消选中(选择并取消选择全部)复选框列表时遇到两个问题:

  1. 父复选框不会选中/取消选中.
  2. 我只需要选择孩子.

例:

http://jsfiddle.net/TheFiddler/v6wmV/ ,

$(document).ready(function(){
    $('.all').toggle(
        function() {
            $('.check').find('input[type=checkbox]').attr('checked', true);
        },
        function() {
            $('.check').find('input[type=checkbox]').attr('checked', false);
        });
});
Run Code Online (Sandbox Code Playgroud)

现在,我在一个类上有事件,因为这个类由两个复选框列表共享,所以它们都会切换.

sha*_*ruz 6

演示

$(document).ready(function(){ 
    $('.all').click(function() {
        var $checkboxes = $(this).parent().find('input[type=checkbox]');
        $checkboxes.prop('checked', $(this).is(':checked'));
    });     
});  
Run Code Online (Sandbox Code Playgroud)

HTML

<div class="check">
      <input type="checkbox" class="all" /> Check/Uncheck all<br />
      <input type="checkbox" /> 1<br />
      <input type="checkbox" /> 2<br />
      <input type="checkbox" /> 3<br />
      <input type="checkbox" /> 4<br />
      <input type="checkbox" /> 5<br />
      <input type="checkbox" /> 6<br />
      <input type="checkbox" /> 7
</div>
...
Run Code Online (Sandbox Code Playgroud)