简体   繁体   English

使用 jquery 和 Bootstrap 选中/取消选中所有复选框的问题

[英]Problems with checking/unchecking all checkboxes with jquery and Bootstrap

I have a problem with checking all my checkboxes with Jquery.我在使用 Jquery 检查所有复选框时遇到问题。

With this code, I can check or uncheck all my boxes.使用此代码,我可以选中或取消选中我的所有框。
But if I check one bow manually that box leaves the group and don't follow the "checkAll" box anymore.但是,如果我手动检查一个弓,该框就会离开该组并且不再遵循“checkAll”框。 Why is that?这是为什么?

First try the first checkbox.. then check another box and then try the first again.首先尝试第一个复选框..然后选中另一个框,然后再试第一个。

 $('#checkAll').on('change', function(){ if($('#checkAll').is(":checked")){ //CHECK ALL BOXES $('.checkbox').attr('checked', true); } else{ //UNCHECK ALL BOXES $('.checkbox').attr('checked', false); } });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input class="" type="checkbox" id="checkAll"> <input class="checkbox" type="checkbox"> <input class="checkbox" type="checkbox"> <input class="checkbox" type="checkbox"> <input class="checkbox" type="checkbox"> <input class="checkbox" type="checkbox">

please use this code:请使用此代码:

$('#checkAll').on('change', function(){

   if($('#checkAll').is(":checked")){
       //CHECK ALL BOXES
       $('.checkbox').prop('checked', true);
   }
   else{
       //UNCHECK ALL BOXES
       $('.checkbox').prop('checked', false);
   }
});

enjoy!请享用!

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

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