點擊「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');
});
}
});