jQuery 检查“这个”复选框是否被选中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8850755/
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 'this' checkbox is checked
提问by Zabs
How can I work out if 'this' checkbox has been checked using the this command?
如何确定是否使用此命令选中了“this”复选框?
I have the following but not sure how to implement this (so it only checks the single checkbox selected rather than all on the page
我有以下但不确定如何实现这一点(所以它只检查选中的单个复选框而不是页面上的所有复选框
if ($('input[type="checkbox"]').is(':checked')
回答by devnull69
What about this?
那这个呢?
if($(this).is(':checked'))
回答by lonesomeday
Presumably you mean this
in the context of an event handler... In which case, just test the checked
property:
大概你的意思是this
在事件处理程序的上下文中......在这种情况下,只需测试checked
属性:
$('input[type="checkbox"]').click(function() {
if (this.checked) {
// checkbox clicked is now checked
}
});
Note that you can do the same with the following methods:
请注意,您可以使用以下方法执行相同操作:
$(this).is(':checked')
$(this).prop('checked')
$(this).is(':checked')
$(this).prop('checked')
However, this.checked
avoids creating a new jQuery object, so is far more efficient (and quicker to code). is
is the slowest: it takes twice as long as prop
and approximately 100 times as long as the simple property lookup.
但是,this.checked
避免创建新的 jQuery 对象,因此效率更高(并且编码更快)。is
是最慢的:它花费的prop
时间是简单属性查找的两倍和大约 100 倍。
回答by robasta
You need to use the ID selector. The input[type="checkbox"] selector will return 'all checkboxes.
您需要使用 ID 选择器。input[type="checkbox"] 选择器将返回“所有复选框”。
if ($('#Checkbox1').is(':checked'))
where Checkbox1 is the ID of the checkbox
其中 Checkbox1 是复选框的 ID
回答by Ayman Safadi
Look into prop()
: http://api.jquery.com/prop/
查看prop()
:http: //api.jquery.com/prop/
The docs provide multiple of better ways to check for a "checked" checkbox.
文档提供了多种更好的方法来检查“已选中”复选框。
elem.checked | true (Boolean) Will change with checkbox state
$(elem).prop("checked") | true (Boolean) Will change with checkbox state
elem.getAttribute("checked") | "checked" (String) Initial state of the checkbox; does not change
$(elem).attr("checked")(1.6) | "checked" (String) Initial state of the checkbox; does not change
$(elem).attr("checked")(1.6.1+) | "checked" (String) Will change with checkbox state
$(elem).attr("checked")(pre-1.6) | true (Boolean) Changed with checkbox state
回答by YuS
Did you mean like this (0 is index):
您的意思是这样吗(0 是索引):
if ($(':checkbox').get(0).is(':checked'))
Or like this:
或者像这样:
if ($(this).is(':checked'))
回答by Robbo_UK
try the following, where you know the id
尝试以下操作,其中您知道 ID
$('#edit-checkbox-id').is(':checked');
or if you are already refering to the item with this keyword
或者如果您已经使用此关键字引用了该项目
$(this).is(':checked');
回答by Salvatore Di Fazio
I use the following:
我使用以下内容:
$(this).find('input[type="checkbox"]:checked')
this example returns all the checkbox was checked
此示例返回所有选中的复选框