简体   繁体   中英

How to uncheck a group of checkbox in JQuery/JavaScript

I'm trying to keep either All and a group of checkboxes (1, 2, 3) unchecked. For example, when All is unchecked and 1 is checked, if I click All , 1 should be unchecked, and vice versa.

 <input type="checkbox" name="checkAll" class="selectAll" /> All <br/> <input type="checkbox" name="checkuser" class="cb_box " /> 1 <input type="checkbox" name="checkuser" class="cb_box " /> 2 <input type="checkbox" name="checkuser" class="cb_box " /> 3 

I tried in this way but it doesn't work. Any suggestion?

$(function () {
 $('.selectAll').click(function () {
   $('input[name=checkuser]').attr('unchecked', $(this).attr('checked'));
});

Use this approach

Function prop along with event change

$('input[name="checkuser"]').prop('checked', $(this).is(':checked'));

 $(function() { $('.selectAll').change(function() { $('input[name="checkuser"]').prop('checked', $(this).is(':checked')); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="checkbox" name="checkAll" class="selectAll" /> All <br/> <input type="checkbox" name="checkuser" class="cb_box " /> 1 <input type="checkbox" name="checkuser" class="cb_box " /> 2 <input type="checkbox" name="checkuser" class="cb_box " /> 3 

Resource

$('.selectAll').click(function(){
     $(':checkbox.cb_box').prop('checked', this.checked);    
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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