我想让选择所有的复选框,我陷入了一些问题。如何在所有项目未选中时取消勾选“全选”复选框?
如何取消“全选”复选框,当所有项目选中?
这是我试过的代码。
<input id="select-all" type="checkbox" name="select-all-cam">
<span class="txt-label">Select All</span>
<div class="list">
<ul>
<li>
<input id="cam-1" type="checkbox" name="select-cam">
<label for="cam-1">item1</label>
</li>
<li>
<input id="cam-2" type="checkbox" name="select-cam">
<label for="cam-2">item2</label>
</li>
</ul>
</div>
$('#select-all').click(function(event) {
if (this.checked) {
$('.list input[type="checkbox"]').each(function() {
this.checked = true;
});
} else {
$('.list input[type="checkbox"]').each(function() {
this.checked = false;
});
}
});
演示:https://fiddle.jshell.net/0j19t3g5/