[英]How to uncheck only one checkbox in list of checkboxes?
如果我單擊 select all,我有一個將被檢查的產品列表。 我想要做的是當所有復選框都被選中並且我只取消選中一個復選框時,它應該只取消選中單擊的元素而不是所有元素。
$('#selectAll').change(function () {
$('#pfilters li input').prop('checked', this.checked);
});
你是這樣的意思嗎?
$('#selectAll').change(function () { $('#pfilters li input').prop('checked', this.checked); }); function isAllSelected() { return $('#pfilters li input').length === $('#pfilters li input:checked').length; } $('#pfilters li input').on('change', function() { $('#selectAll').prop('checked', isAllSelected()); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form> <ul> <li> <label><input type="checkbox" id="selectAll"/>Select All</label> </li> </ul> <ul id="pfilters"> <li><label><input type="checkbox">Select Product 1</label></li> <li><label><input type="checkbox">Select Product 2</label></li> <li><label><input type="checkbox">Select Product 3</label></li> </ul> </form>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.