Javascript jQuery 查看是否选中了任何复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4086957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery see if any or no checkboxes are selected
提问by bba
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 anyof the checkboxes are selected (i.e 1 or more), and I need to see if noneare selected. Basically I need two separate functions that answer these two questions. Help would be appreciated. Thanks!
但是我遇到了以下问题 - 给定一个表单 ID,我需要查看是否选择了任何复选框(即 1 个或多个),并且我需要查看是否没有选择任何复选框。基本上我需要两个独立的函数来回答这两个问题。帮助将不胜感激。谢谢!
Actually, I would just need a function to tell me if noneare selected. Knowing this would answer the other question.
实际上,我只需要一个函数来告诉我是否没有选择。知道这一点将回答另一个问题。
回答by rahul
You can use something like this
你可以使用这样的东西
if ($("#formID input:checkbox:checked").length > 0)
{
// any one is checked
}
else
{
// none is checked
}
回答by Michael Logutov
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
}
回答by Sarfraz
回答by James Drinkard
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.
});
回答by Acheme Paul
Without using 'length' you can do it like this:
不使用“长度”,你可以这样做:
if ($('input[type=checkbox]').is(":checked")) {
//any one is checked
}
else {
//none is checked
}
回答by Rajesh Omanakuttan
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.
Rahul 的回答最适合您的问题。无论如何,如果您要检查一组复选框而不是表单中的所有复选框,您可以选择它。
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:
为您要检查的所有复选框输入一个类名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
,假设一个或多个复选框被选中,类名被选中,test_check
如果返回则没有选中false
。
Hope it helps someone. Thanks :)-
希望它可以帮助某人。谢谢 :)-
回答by Nick Craver
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:
查找给定表单中的复选框,查看是否存在:checked
并返回true
是否存在(因为否则长度将为 0)。为了更清楚一点,这里是非布尔转换版本:
function howManyAreChecked(formID) {
return $('#'+formID+' input[type=checkbox]:checked').length;
}
This would return a count of how many were checked.
这将返回检查了多少的计数。