[英]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.