我如何更正此 jquery / js 复选框代码

How can i correct this jquery / js checkbox code

我一直很难理解这些代码。

复选框代码的功能如下:

有一个全局复选框(全部选中)和子(单个)复选框。如果选中(全局)复选框,则也选中所有(子)复选框,将显示 div,如果未选中全局,则取消选中(子)复选框和 div 将隐藏(jquery 隐藏和显示)。将显示选中复选框的数量。

这就是问题;如果未选中子复选框,则全局复选框仍保持选中状态,如果选中所有子复选框,则也应立即选中全局复选框。

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">    </script>
 <div id="mydiv" style="display:none;">RESTORE | DELETE
 <span>Checked: </span>
 <span id="counter"></span>
 </div>
<input type="checkbox" id="global">
<br>
<input type="checkbox" class="child">
<input type="checkbox" class="child">
<input type="checkbox" class="child">
<input type="checkbox" class="child">
<input type="checkbox" class="child">
<input type="checkbox" class="child">
<input type="checkbox" class="child">

    $(document).ready(function() {
  $('#global').click(function() {
    $('.child').prop('checked', $(this).is(':checked'));

    if ($(this).is(':checked'))
      $('#mydiv').show();
    else
      $('#mydiv').hide();

    count();
  });

  $('.child').change(function() {
    var checkedLength = $('.child:checked').length;
    if (checkedLength > 0)
      $('#mydiv').show();
    else
      $('#mydiv').hide();
    count();
  });
});

var count = function() {
   var i = $('input.child:checked').length;
   $('#counter').html(i);
}

感谢大家的支持。 提前致谢。

尝试:

$(document).ready( function(){
    $('#global').change( function(){
        var bIsChecked = $(this).is(':checked');
        $('.child').prop('checked', bIsChecked );
        $('#mydiv')[ bIsChecked ? 'show' : 'hide' ]();
        count();
    });

    $('.child').change( function(){
        var n = count();
        $('#mydiv')[ n>0 ? 'show' : 'hide' ]();
        $('#global')[0].checked = n==$('.child').length ;
    });
});

var count =function(){
   var n = $('.child:checked').length;
   $('#counter').html( n );
   return n
}

$(document).ready(function() {
  var div = $('#mydiv'),
      global = $('#global'),
      childchecks = $(':checkbox.child');

  global.on('change', function() {
    childchecks.prop('checked', this.checked);
    var how = this.checked ? 'show' : 'hide';
    div[how]();
  });

  childchecks.on('change', function() {
    global.prop('checked', childchecks.length === childchecks.filter(':checked').length );
    var how = childchecks.length === childchecks.filter(':checked').length ? 'show' : 'hide';
    div[how]();
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">    </script>
 <div id="mydiv" style="display:none;">RESTORE | DELETE
 <span>Checked: </span>
 <span id="counter"></span>
 </div>
<input type="checkbox" id="global">
<br>
<input type="checkbox" class="child">
<input type="checkbox" class="child">
<input type="checkbox" class="child">
<input type="checkbox" class="child">
<input type="checkbox" class="child">
<input type="checkbox" class="child">
<input type="checkbox" class="child">

将 #global 更改为 .global

这应该有效。只需要一个事件。

$(document).ready(function() {
  var child = $('.child');
  var global = $('.global');

  $('input').on("change", function() {
    //check if checkbox is the global one
    if($(this).hasClass("global")) {
      //if yes, then set all childs to checked true, if not, then to false
      ($(this).prop('checked')) ? child.prop('checked', true) : child.prop('checked', false);
    } else {
      var oneChecked = false;
      //every change on a checkbox go though all childboxes an check if on of them is checked
      child.each(function() {
        if($(this).prop('checked') == true) {
          oneChecked = true;
        }
      });
      //if one was checked global has to be checked, if no one was checked it has to be false
      (oneChecked) ? global.prop('checked', true) : global.prop('checked', false);
    } 
  });
});