Max*_*270 2 html javascript checkbox jquery
我是JS的新手,我有一个带有2个过滤器的页面,我被困住了,我想:
如果检查过滤器的每个其他复选框,则自动检查(全部)复选框.
当用户取消选中复选框时,会自动取消选中(全部)复选框.
我正在尝试以下代码,但它并没有真正起作用......
HTML:
<div id="filter1">
<form action='#'>
<input type="checkbox" value="all" class="select-all" checked>(All)
<br>
<input type="checkbox" value="v1" class="checkboxlistitem" checked>F1: Value1
<br>
<input type="checkbox" value="v2" class="checkboxlistitem" checked>F1: Value2
<br>
<input type="checkbox" value="v3" class="checkboxlistitem" checked>F1: Value3
<br>
<input type="checkbox" value="v4" class="checkboxlistitem" checked>F1: Value4
<br>
<input type="submit" value="Apply">
</form>
</div>
<div id="filter2">
<form action='#'>
<input type="checkbox" value="all" class="select-all" checked>(All)
<br>
<input type="checkbox" value="v1" class="checkboxlistitem" checked>F2: Value1
<br>
<input type="checkbox" value="v2" class="checkboxlistitem" checked>F2: Value2
<br>
<input type="checkbox" value="v3" class="checkboxlistitem" checked>F2: Value3
<br>
<input type="checkbox" value="v4" class="checkboxlistitem" checked>F2: Value4
<br>
<input type="submit" value="Apply">
</form>
</div>
Run Code Online (Sandbox Code Playgroud)
JS:
$(".checkboxlistitem").change(function() {
$(".select-all").prop("checked", $(".checkboxlistitem:checked").length == $(".checkboxlistitem").length);});
Run Code Online (Sandbox Code Playgroud)
的jsfiddle:
https://jsfiddle.net/Max06270/5scgbnww/
如果您需要澄清,请告诉我,提前谢谢,Max
你的jquery选择器太通用了,所以它们影响了两种形式.下面应该是你想要的一个工作示例:
$(".select-all").change(function () {
$(this).siblings().prop('checked', $(this).prop("checked"));
});
$(".checkboxlistitem").change(function() {
var checkboxes = $(this).parent().find('.checkboxlistitem');
var checkedboxes = checkboxes.filter(':checked');
if(checkboxes.length === checkedboxes.length) {
$(this).parent().find('.select-all').prop('checked', true);
} else {
$(this).parent().find('.select-all').prop('checked', false);
}
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="filter1">
<form action='#'>
<input type="checkbox" value="all" class="select-all" checked>(All)<br>
<input type="checkbox" value="v1" class="checkboxlistitem" checked>F1: Value1<br>
<input type="checkbox" value="v2" class="checkboxlistitem" checked>F1: Value2<br>
<input type="checkbox" value="v3" class="checkboxlistitem" checked>F1: Value3<br>
<input type="checkbox" value="v4" class="checkboxlistitem" checked>F1: Value4<br>
<input type="submit" value="Apply">
</form>
</div>
<br>
<div id="filter2">
<form action='#'>
<input type="checkbox" value="all" class="select-all" checked>(All)<br>
<input type="checkbox" value="v1" class="checkboxlistitem" checked>F2: Value1<br>
<input type="checkbox" value="v2" class="checkboxlistitem" checked>F2: Value2<br>
<input type="checkbox" value="v3" class="checkboxlistitem" checked>F2: Value3<br>
<input type="checkbox" value="v4" class="checkboxlistitem" checked>F2: Value4<br>
<input type="submit" value="Apply">
</form>
</div>Run Code Online (Sandbox Code Playgroud)
https://jsfiddle.net/0epu6Lnn/