我正在尝试这样做,因此单击它会检查以下三个规则:
如果所有内容都未选中,请选中“所有”复选框。
如果选中“所有”复选框,则取消选中其他所有复选框。
如果选中了控制台(至少其中之一),则不要执行上述任何一项。
不幸的是,它仅循环一次并停止工作,并且从不满足任何其他要求。有人可以告诉我我所缺少的吗?
谢谢您的协助。
$('#platform-container').click(function() {
if((`input[type=checkbox][name=platform]:checked`).length > 0){
$('#platform-all').attr("checked", status)
}
else if($('#platform-all').is(':checked')){
$('.single').attr("unchecked", status)
}
else{
$('.single').is(':checked')
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="button-styles platforms-list">
<button id="platform-button" class="button">
Platforms
</button>
<form id="platform-container" class="param-container">
<div class="checkbox-container">
<input type="checkbox" id="platform-all" class="platform" name="platform" value="4,18,1,5,6,7,8" />
<label for="platform">All</label
>
</div>
<div class="checkbox-container">
<input
type="checkbox"
id="platform-switch"
class="single platform"
name="platform"
value="7"
checked="true"
/>
<label for="platform-switch"
>Nintendo Switch</label
>
</div>
<div class="checkbox-container">
<input
type="checkbox"
id="platform-ps4"
class="single platform"
name="platform"
value="18"
/>
<label for="platform-ps4"
>Playstation 4</label
>
</div>
<div class="checkbox-container">
<input
type="checkbox"
id="platform-xb1"
class="single platform"
name="platform"
value="1"
/>
<label for="platform-xb1"
>Xbox One</label
>
</div>
<div class="checkbox-container">
<input
type="checkbox"
id="platform-3ds"
class="single platform"
name="platform"
value="8"
/>
<label for="platform-3ds"
>Nintendo 3DS</label
>
</div>
<div class="checkbox-container">
<input
type="checkbox"
id="platform-pcMasterRace"
class="single platform"
name="platform"
value="4"
/>
<label for="platform-pc"
>PC</label
>
</div>
<div class="checkbox-container">
<input
type="checkbox"
id="platform-linux"
class="single platform"
name="platform"
value="6"
/>
<label for="platform-linux"
>Linux</label
>
</div>
<div class="checkbox-container">
<input
type="checkbox"
id="platform-mac"
class="single platform"
name="platform"
value="5"
/>
<label for="platform-mac">Mac</label>
</form>
</div>
您的代码有多个问题,使您无法实现计划的目标:
name="platform[]"
的情况下,应将该名称视为一个数组项,否则任何较新的输入都将覆盖前一个值。if else
($('.single').is(':checked')
)条件中的第三个选项什么都不做,它所做的只是返回一个布尔值,而实际上并没有改变任何值。status
是什么?看起来像未定义的var,它什么也不做,也不会破坏代码。