点击“select all”复选框后,我想选中所有复选框。 Here`s是代码使用jquery选择div内的所有复选框
<div class="fp">
<div class="fpanel">
<div class="fpblockscontainer">
<span class="fpblockslabel">Merchants</span>
<div class="fpblocks">
<label class="checkbox">
<input class=".select-all2" type="checkbox">Select All</label>
<label class="checkbox">
<input type="checkbox">Apple</label>
<label class="checkbox">
<input type="checkbox">orange</label>
<label class="checkbox">
<input type="checkbox">potato</label>
<label class="checkbox">
<input type="checkbox">Williams-Sonoma</label>
</div>
</div>
在这里,我需要一旦选择了“select.all2”复选框以选择所有的复选框。下面的jQuery代码不起作用。我错在哪里?
$(".select-all2").on("click", function() {
if ($(this).is(':checked')) {
$(this).closest("div.fp").find(':checkbox').each(function() {
$(this).attr('checked', 'checked');
});
} else {
$(this).closest("div.fp").find(':checkbox').each(function() {
$(this).removeAttr('checked');
});
}
});