简体   繁体   English

如果其他使用jquery选中,如何取消选中复选框

[英]How to uncheck checkbox if other checked using jquery

I have tried the below code to uncheck the 1st checkbox if any one of the below are checked. 我已尝试使用以下代码取消选中第一个复选框(如果选中了以下任何一项)。 But not able to revert back to the initial stage ie if nothing is selected the first checkbox will checked. 但是无法还原到初始阶段,即如果未选择任何内容,则将选中第一个复选框。

 $('ol input').on('click', function() { if(this.checked){ $('#all').prop('checked', false); }else{ $('#all').prop('checked', true); } }); 
 ol li{list-style:none;} 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div> <input type="checkbox" id="all" checked>All <ol> <li> <input type="checkbox">1 <input type="checkbox">2 <input type="checkbox">3 <input type="checkbox">4 <input type="checkbox">5 <li> </ol> </div> 

Instead of your current else statement, try else if ($('ol input:checked').length == 0) 除了else if ($('ol input:checked').length == 0) ,请尝试else if ($('ol input:checked').length == 0) ,而不是当前的else语句else if ($('ol input:checked').length == 0)

 $('ol input').on('click', function() { if (this.checked) { $('#all').prop('checked', false); } else if ($('ol input:checked').length == 0) { $('#all').prop('checked', true); } }); 
 ol li { list-style: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div> <input type="checkbox" id="all" checked>All <ol> <li> <input type="checkbox">1 <input type="checkbox">2 <input type="checkbox">3 <input type="checkbox">4 <input type="checkbox">5 <li> </ol> </div> 

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

相关问题 如果选中jquery,请取消选中复选框 - Uncheck checkbox if checked with jquery jQuery 取消选中从同一行选中的另一个复选框 - jQuery Uncheck other checkbox on one checked from same row 如果已经选中了其他复选框,如何使用javascript自动取消选中复选框? - How to automatically uncheck a checkbox with javascript if other is already checked? 除“全部”以外的其他选项时,如何取消选中复选框 - How to uncheck a checkbox when checked option is other than 'All' 使用Jquery取消选中复选框 - Uncheck a Checkbox using Jquery 如何在选中单选按钮时取消选中复选框,以及在使用Javascript选中复选框时如何取消选中单选按钮 - How to uncheck checkbox when radio button is checked and uncheck radio button when checkboxes are checked using Javascript 如果选中了表格复选框,则取消选中其他表格复选框吗? - If table checkbox is checked, uncheck other table checkboxes? 如何使用 jquery 取消选中复选框 - how to uncheck a checkbox with jquery jQuery逻辑问题。 如果选中了其他复选框,则需要取消选中它,反之亦然 - Problem with jQuery logic. Need to uncheck one checkbox if other is checked, vice versa 如何选中另一个复选框时取消选中复选框? - How to uncheck a checkbox when another one is checked?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM