Javascript 检查是否至少使用 jQuery 选中了一个复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8369904/
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
Check if at least one checkbox is checked using jQuery
提问by Kate Wintz
I have five checkboxes. Using jQuery, how do I check if at least one of them is checked?
我有五个复选框。使用 jQuery,如何检查是否至少检查了其中之一?
<input type="checkbox" name="service[]">
<br />
<input type="checkbox" name="service[]">
<br />
<input type="checkbox" name="service[]">
<br />
<input type="checkbox" name="service[]">
<br />
<input type="checkbox" name="service[]">
回答by Andy E
is()
can do this, and is arguably the only acceptable use of is(":checked")
:
is()
可以做到这一点,并且可以说是唯一可以接受的用法is(":checked")
:
From the jQuery docs, http://api.jquery.com/is/:
Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments.
alert($("input[name='service[]']").is(":checked"));
Example: http://jsfiddle.net/AndyE/bytVX/1/(based on the fiddle by Brandon Gano)
从 jQuery 文档,http: //api.jquery.com/is/:
根据选择器、元素或 jQuery 对象检查当前匹配的一组元素,如果这些元素中至少有一个与给定的参数匹配,则返回 true。
alert($("input[name='service[]']").is(":checked"));
示例:http: //jsfiddle.net/AndyE/bytVX/1/(基于Brandon Gano的小提琴)
Alternatively, and potentially faster, you can pass a function to is()
:
或者,并且可能更快,您可以将函数传递给is()
:
$("input[name='service[]']").is(function () {
return this.checked;
});
回答by Brandon Gano
Edit:The original solution in this answer is inefficient and should not be used. Please see the revised solution based on comments and examples from other answers to this question.
编辑:此答案中的原始解决方案效率低下,不应使用。请参阅基于对此问题的其他答案的评论和示例的修订解决方案。
The original (bad) solution follows:
原始(坏)解决方案如下:
// DO NOT USE; SEE BELOW
$('button').click(function () {
var atLeastOneIsChecked = false;
$('input:checkbox').each(function () {
if ($(this).is(':checked')) {
atLeastOneIsChecked = true;
// Stop .each from processing any more items
return false;
}
});
// Do something with atLeastOneIsChecked
});
The use of .each() is redundant in this example, as .is() can be used directly on the set of objects rather than manually iterating through each one. A more efficient solution follows:
在这个例子中,.each() 的使用是多余的,因为 .is() 可以直接在一组对象上使用,而不是手动迭代每个对象。一个更有效的解决方案如下:
$('button').click(function () {
var atLeastOneIsChecked = $('input:checkbox').is(':checked');
// Do something with atLeastOneIsChecked
});
Note that one of the comments indicates a strong dislike for $(this).is(':checked')
. To clarify, there is nothing wrong with is(':checked')
in cases where you are testing a set of objects. That said, calling is(':checked')
on a single item is much less efficient than calling .checked
on the same item. It also involves an unnecessary call to the $ function.
请注意,其中一条评论表明非常不喜欢$(this).is(':checked')
。澄清一下,is(':checked')
在测试一组对象的情况下没有任何问题。也就是说,调用is(':checked')
单个项目比调用.checked
同一项目效率低得多。它还涉及对 $ 函数的不必要调用。
回答by jabclab
This should do the trick:
这应该可以解决问题:
function isOneChecked() {
return ($('[name="service[]"]:checked').length > 0);
}
回答by island205
Another answer:
另一个答案:
!!$("[type=checkbox]:checked").length
or
或者
!!$("[name=service[]]:checked").length
It depends on what you want.
这取决于你想要什么。
回答by classifieds.pkbazaar
You should try like this....
你应该像这样尝试......
var checkboxes = $("input[type='checkbox']"),
submitButt = $("input[type='submit']");
checkboxes.click(function() {
submitButt.attr("disabled", !checkboxes.is(":checked"));
});
});
回答by vaibhav kulkarni
you need to check if checkbox is checked or not.
您需要检查复选框是否被选中。
$("#select_all").click(function(){
var checkboxes = $("input[type='checkbox']");
if(checkboxes.is(":checked"))
alert("checked");
else
alert("select at least one;
});
回答by Raynos
var checkboxes = document.getElementsByName("service[]");
if ([].some.call(checkboxes, function () { return this.checked; })) {
// code
}
What you want is simple, get all the elements with the name, then run some code if some of those elements are checked.
您想要的很简单,获取所有具有名称的元素,然后在检查其中一些元素时运行一些代码。
No need for jQuery.
不需要 jQuery。
You may need an ES5 shim for legacy browsers though
不过,对于旧版浏览器,您可能需要 ES5 垫片
回答by Anu
You can do the following way. Initially set a variable, lets say checked
as false
. Then set it to true
if the following condition met. Use an if
statement to check the variable. Take note: Here submit
is the id
of the button, main
is the id of the form.
您可以通过以下方式进行。最初设置一个变量,让我们说checked
as false
。true
如果满足以下条件,则将其设置为。使用if
语句检查变量。注意:这里submit
是id
按钮的,main
是表单的id。
$("#submit").click(function() {
var checked = false;
if (jQuery('#main input[type=checkbox]:checked').length) {
checked = true;
}
if (!checked) {
//Do something
}
});
回答by Zain Khan
var atLeastOneIsChecked = $('input[name="service[]"]:checked').length > 0;