简体   繁体   English

选择全部并取消选择所有不在第二次工作

[英]Select all & Unselect all not working in the second time

I want select & unselect all using button. 我想要使​​用按钮选择和取消全部选择。

The first time I select and unselect the code below works, but the second time, my button is not working. 我第一次选择和取消选择下面的代码,但第二次,我的按钮不起作用。

HTML : HTML:

<button type="button" id="selectAll" class="main"> Select </button>
<button type="button" id="unselectAll" class="main"> UnSelect </button>

jQuery : jQuery:

$("#selectAll").on("click", function() {

  $("#example tr").each(function() {
    $(this).find("input").attr('checked', true);
  });
});

$("#unselectAll").on("click", function() {

  $("#example tr").each(function() {
    $(this).find("input").removeAttr('checked');
  });
});

JSFiddle 的jsfiddle

How can I solve this? 我怎么解决这个问题?

Use prop() instead of attr() and you can combine them together something like: 使用prop()而不是attr(),你可以将它们组合在一起,如:

 $("#selectAll, #unselectAll").on("click", function() { var selectAll = this.id === 'selectAll'; // no need for `each` ... jQuery will do that internally already $("#example tbody :checkbox").prop('checked', selectAll); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button type="button" id="selectAll" class="main"> Select </button> <button type="button" id="unselectAll" class="main"> UnSelect </button> <table id="example" class="myclass" /> <thead> <tr> <th> </th> <th>Name</th> <th>Company</th> <th>Employee Type</th> <th>Address</th> <th>Country</th> </tr> </thead> <tbody> <tr> <td> <input type="checkbox" /> </td> <td>varun</td> <td>TCS</td> <td>IT</td> <td>San Francisco</td> <td>US</td> </tr> <tr> <td> <input type="checkbox" /> </td> <td>Rahuk</td> <td>TCS</td> <td>IT</td> <td>San Francisco</td> <td>US</td> </tr> <tr> <td> <input type="checkbox" /> </td> <td>johm Doe</td> <td>TCS</td> <td>IT</td> <td>San Francisco</td> <td>US</td> </tr> <tr> <td> <input type="checkbox" /> </td> <td>Sam</td> <td>TCS</td> <td>IT</td> <td>San Francisco</td> <td>US</td> </tr> <tr> <td> <input type="checkbox" /> </td> <td>Lara</td> <td>TCS</td> <td>IT</td> <td>San Francisco</td> <td>US</td> </tr> <tr> <td> <input type="checkbox" /> </td> <td>Jay</td> <td>TCS</td> <td>IT</td> <td>San Francisco</td> <td>US</td> </tr> <tr> <td> <input type="checkbox" /> </td> <td>Tom</td> <td>TCS</td> <td>IT</td> <td>San Franciso</td> <td>US</td> </tr> </tbody> </table> 

Use prop instead of attr and removeAttr per this link 每个链接使用prop而不是attrremoveAttr

Sample: 样品:

 $("#selectAll").on("click", function() { $("#example tr").each(function() { $(this).find("input").prop("checked", true); }); }); $("#unselectAll").on("click", function() { $("#example tr").each(function() { $(this).find("input").prop("checked", false); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button type="button" id="selectAll" class="main"> Select </button> <button type="button" id="unselectAll" class="main"> UnSelect </button> <table id="example" class="myclass" /> <thead> <tr> <th> </th> <th>Name</th> <th>Company</th> <th>Employee Type</th> <th>Address</th> <th>Country</th> </tr> </thead> <tbody> <tr> <td> <input type="checkbox" /> </td> <td>varun</td> <td>TCS</td> <td>IT</td> <td>San Francisco</td> <td>US</td> </tr> <tr> <td> <input type="checkbox" /> </td> <td>Rahuk</td> <td>TCS</td> <td>IT</td> <td>San Francisco</td> <td>US</td> </tr> <tr> <td> <input type="checkbox" /> </td> <td>johm Doe</td> <td>TCS</td> <td>IT</td> <td>San Francisco</td> <td>US</td> </tr> <tr> <td> <input type="checkbox" /> </td> <td>Sam</td> <td>TCS</td> <td>IT</td> <td>San Francisco</td> <td>US</td> </tr> <tr> <td> <input type="checkbox" /> </td> <td>Lara</td> <td>TCS</td> <td>IT</td> <td>San Francisco</td> <td>US</td> </tr> <tr> <td> <input type="checkbox" /> </td> <td>Jay</td> <td>TCS</td> <td>IT</td> <td>San Francisco</td> <td>US</td> </tr> <tr> <td> <input type="checkbox" /> </td> <td>Tom</td> <td>TCS</td> <td>IT</td> <td>San Franciso</td> <td>US</td> </tr> </tbody> 

hope u will like it 希望你会喜欢它

 $("document").ready(function() { $("#mcb").change(function() { $("#example tbody :checkbox").prop("checked", $("#mcb").prop("checked")); // choose by element or id $("#example tbody #scb").prop("checked", $("#mcb").prop("checked")); }); $("[id='scb']").change(function() { if($(this).prop("checked")==false) { $("#mcb").prop("checked",false); } var tf=true; $("#example tbody #scb").each(function() { if($(this).prop("checked")==false) {tf=false;} }); $("#mcb").prop("checked",tf); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table id="example"/> <thead> <tr> <th><input id="mcb" type="checkbox" /></th> <th>Name</th> <th>Company</th> <th>Employee Type</th> <th>Address</th> <th>Country</th> </tr> </thead> <tbody> <tr> <td> <input id="scb" type="checkbox" /> </td> <td>varun</td> <td>TCS</td> <td>IT</td> <td>San Francisco</td> <td>US</td> </tr> <tr> <td> <input id="scb" type="checkbox" /> </td> <td>Rahuk</td> <td>TCS</td> <td>IT</td> <td>San Francisco</td> <td>US</td> </tr> <tr> <td> <input id="scb" type="checkbox" /> </td> <td>johm Doe</td> <td>TCS</td> <td>IT</td> <td>San Francisco</td> <td>US</td> </tr> <tr> <td> <input id="scb" type="checkbox" /> </td> <td>Sam</td> <td>TCS</td> <td>IT</td> <td>San Francisco</td> <td>US</td> </tr> <tr> <td> <input id="scb" type="checkbox" /> </td> <td>Lara</td> <td>TCS</td> <td>IT</td> <td>San Francisco</td> <td>US</td> </tr> <tr> <td> <input id="scb" type="checkbox" /> </td> <td>Jay</td> <td>TCS</td> <td>IT</td> <td>San Francisco</td> <td>US</td> </tr> <tr> <td> <input id="scb" type="checkbox" /> </td> <td>Tom</td> <td>TCS</td> <td>IT</td> <td>San Franciso</td> <td>US</td> </tr> </tbody> </table> 

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

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