简体   繁体   English

使用jQuery选择和取消选择复选框

[英]select and deselect checkboxes with jquery

See the following code: 请参见以下代码:

  $(document).ready(function() { $('.checkbox-group .parents-checkbox .panel-title input').click(function () { $(this).closest('.checkbox-group').find('input[type="checkbox"]').not(this).prop('checked', this.checked).closest('label'); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="panel panel-default item-box-roll checkbox-group"> <div class="panel-heading parents-checkbox"> <h3 class="panel-title"> <input type="checkbox" name="rolls[]" id="selecctall" value="2" checked="">&nbsp;&nbsp;Items </h3> </div> <div class="panel-body child-checkbox"> <input type="checkbox" class="checkbox1" name="rolls[]" value="20" checked="">&nbsp;&nbsp;Items management<br> <input type="checkbox" class="checkbox1" name="rolls[]" value="21" checked="">&nbsp;&nbsp;Add item<br> <input type="checkbox" class="checkbox1" name="rolls[]" value="22" checked="">&nbsp;&nbsp;Edit items<br> </div> </div> 

When i click on first checkbox therefore select all checkbox in child-checkbox div. 当我单击第一个复选框时,请在子复选框div中选中所有复选框。 I want to if selected each of checkbox in child-checkbox div then check first checkbox! 我想在子复选框div中选中每个复选框,然后选中第一个复选框! How can I change the code? 如何更改代码?

You need to listen to the child checkboxes and set the parent checkbox based on whether they have no unchecked boxes . 您需要侦听子复选框,并根据它们是否没有未选中的框来设置父复选框。

JSFiddle: http://jsfiddle.net/TrueBlueAussie/annvy1ed/1/ JSFiddle: http : //jsfiddle.net/TrueBlueAussie/annvy1ed/1/

$('.child-checkbox :checkbox').change(function(){
    $('.checkbox-group .parents-checkbox .panel-title :checkbox').prop('checked', $('.child-checkbox :checkbox').not(':checked').length == 0);
});

The selectors can all be simplified, but this will answer your basic question. 选择器都可以简化,但这将回答您的基本问题。

You can shorten it to: 您可以将其缩短为:

$('.checkbox-group .parents-checkbox .panel-title :checkbox').prop('checked', !$('.child-checkbox :checkbox:not(:checked)').length);

http://jsfiddle.net/TrueBlueAussie/annvy1ed/3/ http://jsfiddle.net/TrueBlueAussie/annvy1ed/3/

You can shorten it a lot more, eg if you can make use of your ID selector, but your code indicates you may have multiple groups of checkboxes on the page. 您可以将其缩短很多,例如,如果可以使用ID选择器,但是代码表明您在页面上可能具有多组复选框。

If there are multiple groups you will need to also use closest again in the new code: 如果多个组,你将也需要使用closest新代码再次:

eg http://jsfiddle.net/TrueBlueAussie/annvy1ed/9/ 例如http://jsfiddle.net/TrueBlueAussie/annvy1ed/9/

$('.child-checkbox :checkbox').change(function(){
    var $group = $(this).closest('.checkbox-group');
    $group.find('.parents-checkbox .panel-title :checkbox').prop('checked', !$group.find('.child-checkbox :checkbox:not(:checked)').length);
});

Notes: 笔记:

Update "any" child checked: 更新“任何”孩子检查:

If you want to tick the parent if any of the child boxes is checked, reverse the logic: 如果你想打勾的家长如有的孩子箱检查,反向的逻辑:

http://jsfiddle.net/TrueBlueAussie/annvy1ed/5/ http://jsfiddle.net/TrueBlueAussie/annvy1ed/5/

$('.child-checkbox input').change(function(){
    $('.checkbox-group .parents-checkbox .panel-title input').prop('checked', $('.child-checkbox input:checked').length);
});

or, to support multiple checkbox groups: 或者,为了支持多个复选框组:

http://jsfiddle.net/TrueBlueAussie/annvy1ed/8/ http://jsfiddle.net/TrueBlueAussie/annvy1ed/8/

$('.child-checkbox :checkbox').change(function(){
    var $group = $(this).closest('.checkbox-group');
    $group.find('.parents-checkbox .panel-title :checkbox').prop('checked', $group.find('.child-checkbox :checkbox:checked').length);
});

The easiest way to do this is to check the length 最简单的方法是检查长度

$(".checkbox1").change(function(){
    if ($('.checkbox1:checked').length == $('.checkbox1').length) {
       //check the parent checkbox
    }
};

Tip: You should use data attributes on your elements to reduce the amount of code and complexity of your JavaScript... 提示:您应该在元素上使用数据属性以减少代码量和JavaScript的复杂性...

 $(document).ready(function() { $('.checkbox-group .parents-checkbox .panel-title input').click(function() { $(this).closest('.checkbox-group').find('input[type="checkbox"]').not(this).prop('checked', this.checked).closest('label'); }); $('.checkbox-group .child-checkbox input').click(function() { var total = $('.checkbox-group .child-checkbox input').length; var checked = $('.checkbox-group .child-checkbox input:checked').length; $('.checkbox-group .parents-checkbox .panel-title input').prop('checked', (total === checked)); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="panel panel-default item-box-roll checkbox-group"> <div class="panel-heading parents-checkbox"> <h3 class="panel-title"> <input type="checkbox" name="rolls[]" id="selecctall" value="2" checked="">&nbsp;&nbsp;Items </h3> </div> <div class="panel-body child-checkbox"> <input type="checkbox" class="checkbox1" name="rolls[]" value="20" checked="">&nbsp;&nbsp;Items management <br> <input type="checkbox" class="checkbox1" name="rolls[]" value="21" checked="">&nbsp;&nbsp;Add item <br> <input type="checkbox" class="checkbox1" name="rolls[]" value="22" checked="">&nbsp;&nbsp;Edit items <br> </div> </div> 

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

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