I know how to see if an individual checkbox is selected or not.
我知道如何查看单个复选框是否被选中。
But Im having trouble with the following - given a form id I need to see if any of the checkboxes are selected (i.e 1 or more), and I need to see if none are selected. Basically I need two separate functions that answer these two questions. Help would be appreciated. Thanks!
但我在以下方面有问题——如果给定一个表单id,我需要查看是否有选中的复选框(I)。e 1或以上),我需要看看是否没有选择。基本上我需要两个独立的函数来回答这两个问题。帮助我们将不胜感激。谢谢!
Actually, I would just need a function to tell me if none are selected. Knowing this would answer the other question.
实际上,我只需要一个函数来告诉我是否没有被选中。知道这个就能回答另一个问题。
7 个解决方案
#1
201
You can use something like this
你可以用这样的东西
if ($("#formID input:checkbox:checked").length > 0)
{
// any one is checked
}
else
{
// none is checked
}
#2
24
JQuery .is
will test all specified elements and return true if at least one of them matches selector:
JQuery .is将测试所有指定的元素,如果其中至少有一个匹配选择器,则返回true:
if ($(":checkbox[name='choices']", form).is(":checked"))
{
// one or more checked
}
else
{
// nothing checked
}
#3
6
You can do this:
你可以这样做:
if ($('#form_id :checkbox:checked').length > 0){
// one or more checkboxes are checked
}
else{
// no checkboxes are checked
}
Where:
地点:
#4
3
You can do a simple return of the .length
here:
你可以在这里简单地返回。length:
function areAnyChecked(formID) {
return !!$('#'+formID+' input[type=checkbox]:checked').length;
}
This look for checkboxes in the given form, sees if any are :checked
and returns true
if they are (since the length would be 0 otherwise). To make it a bit clearer, here's the non boolean converted version:
在给定的表单中查找复选框,查看是否有:勾选并返回true(否则长度为0)。为了让它更清楚一点,这里有一个非布尔转换的版本:
function howManyAreChecked(formID) {
return $('#'+formID+' input[type=checkbox]:checked').length;
}
This would return a count of how many were checked.
这将返回检查了多少次的计数。
#5
2
Rahul's answer is best fit for your question. Anyway, if you have a group of checkboxes to be checked and not all the checkbox in your form, you can go for it.
拉胡尔的回答最适合你的问题。无论如何,如果你有一组复选框要选中,而不是表单中的所有复选框,你可以选择它。
Put a classname for all the checkboxes you want to check, say for example, a classname test_check
and now you can check if any of the checkbox is checked belonging to the group by:
为您想要检查的所有复选框设置一个classname,例如,classname test_check,现在您可以检查是否有任何一个复选框是由:
$("#formID .test_check:checked").length > 0
If it returns true
, assume that one or more checkboxes are checked having the classname test_check
and none checked if returns false
.
如果它返回true,假设检查了一个或多个复选框具有classname test_check,并且没有检查返回false。
Hope it helps someone. Thanks :)-
希望它能帮助一些人。谢谢:)- - - - - -
#6
1
This is what I used for checking if any checkboxes in a list of checkboxes had changed:
这是我用来检查复选框列表中的任何复选框是否已经更改的:
$('input[type="checkbox"]').change(function(){
var itemName = $('select option:selected').text();
//Do something.
});
#7
0
Without using 'length' you can do it like this:
如果不使用“长度”,你可以这样做:
if ($('input[type=checkbox]').is(":checked")) {
//any one is checked
}
else {
//none is checked
}
#1
201
You can use something like this
你可以用这样的东西
if ($("#formID input:checkbox:checked").length > 0)
{
// any one is checked
}
else
{
// none is checked
}
#2
24
JQuery .is
will test all specified elements and return true if at least one of them matches selector:
JQuery .is将测试所有指定的元素,如果其中至少有一个匹配选择器,则返回true:
if ($(":checkbox[name='choices']", form).is(":checked"))
{
// one or more checked
}
else
{
// nothing checked
}
#3
6
You can do this:
你可以这样做:
if ($('#form_id :checkbox:checked').length > 0){
// one or more checkboxes are checked
}
else{
// no checkboxes are checked
}
Where:
地点:
#4
3
You can do a simple return of the .length
here:
你可以在这里简单地返回。length:
function areAnyChecked(formID) {
return !!$('#'+formID+' input[type=checkbox]:checked').length;
}
This look for checkboxes in the given form, sees if any are :checked
and returns true
if they are (since the length would be 0 otherwise). To make it a bit clearer, here's the non boolean converted version:
在给定的表单中查找复选框,查看是否有:勾选并返回true(否则长度为0)。为了让它更清楚一点,这里有一个非布尔转换的版本:
function howManyAreChecked(formID) {
return $('#'+formID+' input[type=checkbox]:checked').length;
}
This would return a count of how many were checked.
这将返回检查了多少次的计数。
#5
2
Rahul's answer is best fit for your question. Anyway, if you have a group of checkboxes to be checked and not all the checkbox in your form, you can go for it.
拉胡尔的回答最适合你的问题。无论如何,如果你有一组复选框要选中,而不是表单中的所有复选框,你可以选择它。
Put a classname for all the checkboxes you want to check, say for example, a classname test_check
and now you can check if any of the checkbox is checked belonging to the group by:
为您想要检查的所有复选框设置一个classname,例如,classname test_check,现在您可以检查是否有任何一个复选框是由:
$("#formID .test_check:checked").length > 0
If it returns true
, assume that one or more checkboxes are checked having the classname test_check
and none checked if returns false
.
如果它返回true,假设检查了一个或多个复选框具有classname test_check,并且没有检查返回false。
Hope it helps someone. Thanks :)-
希望它能帮助一些人。谢谢:)- - - - - -
#6
1
This is what I used for checking if any checkboxes in a list of checkboxes had changed:
这是我用来检查复选框列表中的任何复选框是否已经更改的:
$('input[type="checkbox"]').change(function(){
var itemName = $('select option:selected').text();
//Do something.
});
#7
0
Without using 'length' you can do it like this:
如果不使用“长度”,你可以这样做:
if ($('input[type=checkbox]').is(":checked")) {
//any one is checked
}
else {
//none is checked
}