我试图检查所有元素如果复选框输入类型,我试过不同的解决方案,但它没有工作,我知道这很简单,但我不知道什么是错的我的代码:如何检查所有复选框使用jQuery
<ul class="items">
<li class="item checkall">
<label>
<input type="checkbox" name="item[]" id="checkAll">
<span></span>
<p>Check All Items</p>
</label>
</li>
<li class="item">
<label>
<input type="checkbox" name="item[]" id="checkbox" value="1">
<span></span>
<p>Learn HTML5.1</p>
</label>
</li>
<li class="item">
<label>
<input type="checkbox" name="item[]" id="checkbox" value="2">
<span></span>
<p>Learn PHP7</p>
</label>
</li>
<li class="item">
<label>
<input type="checkbox" name="item[]" id="checkbox" value="4">
<span></span>
<p>Program a Website</p>
</label>
</li>
</ul>
的jQuery:
$(".checkall").click(function() {
$('input:checkbox').not(this).prop('checked', this.checked);
});
首先使用class而不是id。 id必须是唯一的,并使用'#id'选择一个id和'.class'来选择一个类。如果你看起来更好,还有很多像你这样的其他问题http://stackoverflow.com/questions/18537609/jquery-checkbox-check-all; http://stackoverflow.com/questions/15504643/check-uncheck-all-checkboxes-with-another-single-checkbox-use-jquery – Alexis
是的。这很简单。所以,我的问题是:如果你想检查所有复选框,为什么你需要'not(this)'语句? –
使用'change'事件而不是'click' – Satpal