JavaScript this.checked
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7672018/
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
JavaScript this.checked
提问by Simplicity
In JavaScript, if we write the following for example:
在 JavaScript 中,如果我们编写以下示例:
var c = this.checked;
What is checkedhere? Is it just a statethat tells us if a checkbox for example is checked or not? So, can we use it to check that the checkbox is also not checked?
什么是checked在这里吗?这是否只是一种状态,它告诉我们,如果例如复选框被选中或不?那么,我们可以用它来检查复选框是否也没有被选中吗?
回答by Daniel Apps
To use the pseudo selector :checkedwith the jquery object thiswrite:
要:checked在 jquery 对象中使用伪选择器,请this写入:
$(this).is(':checked')
回答by dougajmcdonald
Is it just a state that tells us if a checkbox for example is checked or not? So, can we use it to check that the checkbox is also not checked?
它只是一个状态,告诉我们一个复选框是否被选中?那么,我们可以用它来检查复选框是否也没有被选中吗?
Yes and yes
是和是
回答by James Allardice
Assuming thisrefers to a DOM element which has a checkedproperty (e.g. a checkbox or a radio button) then the checkedproperty will either be trueif the element is checked, or falseif it's not. For example, given this HTML:
假设this引用具有checked属性(例如复选框或单选按钮)的 DOM 元素,那么该checked属性要么是true该元素被选中,要么是未选中false。例如,给定这个 HTML:
<input type="checkbox" id="example">
The following line of JS will return false:
以下 JS 行将返回false:
var c = document.getElementById("example").checked; //False
Note that what you've written is standard JavaScript, not jQuery. If thisrefers to a jQuery object rather than a DOM element, checkedwill be undefined because the jQuery object does not have a checkedproperty. If thisis a jQuery object, you can use .prop:
请注意,您编写的是标准 JavaScript,而不是 jQuery。如果this引用的是 jQuery 对象而不是 DOM 元素,checked则将是未定义的,因为 jQuery 对象没有checked属性。如果this是 jQuery 对象,则可以使用.prop:
var c = this.prop("checked");
回答by Naveed
In jQuery checked is a selector:
The :checked selector works for checkboxes and radio buttons.
:checked 选择器适用于复选框和单选按钮。
There are some ways to check if a checkbox is checked or not:
有一些方法可以检查复选框是否被选中:
For Example:
例如:
$('#checkBox').attr('checked');
or
$('#checkBox').is(':checked');

