我正在尝试为用户构建过滤器,包括选择/取消选择所有功能。Jquery取消选中所有复选框多个表单
我已经发现了很多正确的例子,但是没有一个在演示中有多个过滤器,现在它的行为很奇怪。
当我点击(全部)的过滤器1 IT'IS工作正常,但不是(全部)的过滤器2 ...
的jsfiddle:
https://jsfiddle.net/Max06270/pvtqpn8a/#&togetherjs=2tvV3Mhb7l
HTML:
<div id="filter1">
<form action='#'>
<input type="checkbox" id="select-all" checked>(All)<br>
<!-- Should select/unselect only the F1 checkboxes -->
<input type="checkbox" id="checkbox-f1" checked>F1: Value1<br>
<input type="checkbox" id="checkbox-f1" checked>F1: Value2<br>
<input type="checkbox" id="checkbox-f1" checked>F1: Value3<br>
<input type="checkbox" id="checkbox-f1" checked>F1: Value4<br>
<input type="submit" id="submit-f1" value="Apply">
</form>
</div>
<br>
<div id="filter2">
<form action='#'>
<input type="checkbox" id="select-all" checked>(All)<br>
<!-- Should select/unselect only the F2 checkboxes -->
<input type="checkbox" id="checkbox-f2" checked>F2: Value1<br>
<input type="checkbox" id="checkbox-f2" checked>F2: Value2<br>
<input type="checkbox" id="checkbox-f2" checked>F2: Value3<br>
<input type="checkbox" id="checkbox-f2" checked>F2: Value4<br>
<input type="submit" id="submit-f2" value="Apply">
</form>
</div>
JS:
$("#select-all").change(function() {
$(this).siblings().prop('checked', $(':checkbox').prop("checked"));
});
预先感谢您, 最大
您应该使用一个类'选择-all'而不是ID。 ID的应该是唯一的一个元素。编辑:同样适用于您的输入ID;那些应该也是类。 –