[英]How to check if checkbox is checked inside a particular form?
I created a jQuery function to check if a checkbox is checked like so: 我创建了一个jQuery函数来检查是否选中了复选框:
if ($('input:checkbox:checked').length > 0){ }
I have two set of check boxes in two separate forms. 我有两组独立的两组复选框。 When I use above code it checks for checkboxes in both forms.
当我使用上面的代码时,它会检查两种形式的复选框。 I want the
if
condition only for one form. 我希望
if
条件只适用于一个表单。 How can I achieve it? 我怎样才能实现它?
You can include your form
in selector
by using its id
or class
etc, 您可以使用其
id
或class
等将您的form
包含在selector
,
By using form id 通过使用表单ID
if ($('#yourFormId input:checkbox:checked').length > 0){ }
By using form class 通过使用表单类
if ($('.yourFormClass input:checkbox:checked').length > 0){ }
By using the index of form, eq(0) gives you first form and eq(1) second so on... 通过使用形式索引,eq(0)给出第一个形式,eq(1)秒给...
if ($('form:eq(0) input:checkbox:checked').length > 0) { }
You can use :eq()
selector. 您可以使用
:eq()
选择器。 Try the following: 请尝试以下方法:
if ($('form:eq(0) input[type="checkbox"]:checked').length > 0) { } // first form checkboxes
if ($('form:eq(1) input[type="checkbox"]:checked').length > 0) { } // second form checkboxes
please note that :checkbox
selector is deprecated you can use input[type="checkbox"]
instead. 请注意
:checkbox
不建议使用:checkbox
选择器,您可以使用input[type="checkbox"]
代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.