所以我有一个带有复选框的字段,需要在提交之前选择这些复选框。 问题在于,复选框的数量不是恒定的,因为它取决于从数据库中获取的要检查的值的数量。
基本上,复选框的数量范围为 1 到 9。
我使用ajax发送值,所以我没有在表单中列出“检查项目”,而是在列表中。像这样的事情
Unit 1: <input type="checkbox" name="unit[]" value="1"/>
Unit 2: <input type="checkbox" name="unit[]" value="2"/>
Unit 3: <input type="checkbox" name="unit[]" value="3"/>
Unit 4: <input type="checkbox" name="unit[]" value="4"/>
<input type="submit" value="register units"/>
问题是:如何确保每次提交前所有复选框都会被选中。
最佳答案
像这样:
if($('input[type="checkbox"]:checked').length == $('input[type="checkbox"]').length){
//all checkboxes are checked.
}
您还可以使用 name 属性来使用 $('input[name="unit[]"]'
和 $('input[name="unit[]"]:选中')
关于jquery - 要求选择所有复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22379397/