我创建了一个 jQuery 函数来检查复选框是否被选中,如下所示:
if ($('input:checkbox:checked').length > 0){ }
我在两个不同的表单中有两组复选框。当我使用上面的代码时,它会检查两种形式的复选框。我只想要一种形式的 if
条件。我怎样才能实现它?
最佳答案
您可以使用 id
或 class
等将您的 form
包含在 selector
中,
使用表单id
if ($('#yourFormId input:checkbox:checked').length > 0){ }
通过使用表单类
if ($('.yourFormClass input:checkbox:checked').length > 0){ }
通过使用表格的索引,eq(0) 给出第一个表格,eq(1) 给出第二个…
if ($('form:eq(0) input:checkbox:checked').length > 0) { }
关于javascript - 如何检查特定表单中是否选中了复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11692955/