admin管理员组文章数量:1434908
I've done a lot of searching on the web and found examples that treat $('#checkbox').attr('checked')
as a string and others that treat it as a Boolean.
In my case, I find that this statement works as expected:
$('#AcceptAgreement').attr('checked', false);
But this one does not:
if ($('#AcceptAgreement').attr('checked') == true)
The second statement is false because in fact the value is 'checked'.
So which is it? Is it a string or Boolean value, and how can I read and write it in a reliable manner?
I've done a lot of searching on the web and found examples that treat $('#checkbox').attr('checked')
as a string and others that treat it as a Boolean.
In my case, I find that this statement works as expected:
$('#AcceptAgreement').attr('checked', false);
But this one does not:
if ($('#AcceptAgreement').attr('checked') == true)
The second statement is false because in fact the value is 'checked'.
So which is it? Is it a string or Boolean value, and how can I read and write it in a reliable manner?
Share Improve this question asked Apr 27, 2012 at 21:06 Jonathan WoodJonathan Wood 67.5k82 gold badges305 silver badges533 bronze badges 2-
you almost always want the prop() method and not the attr() method. prop() accepts only a boolean, attr accepts both, but can be slower, or might not be the proper way of accessing the property of an element.
– Ohgodwhy Commented Apr 27, 2012 at 21:10 -
Maybe it is dated back to time when IE allowed checkboxes with
checked='1'
,disabled='true'
but may not on some other browsers – U and me Commented Apr 27, 2012 at 21:20
6 Answers
Reset to default 4Probably you should not use attributes, to change state just use 'checked' property of the dom node
$('#AcceptAgreement')[0].checked = false
if ($('#AcceptAgreement')[0].checked)
This depends on which version of jQuery you are using.
checked
is both a property and an attribute. In older versions of jQuery, .attr()
always returned the property, not the attribute, which was usually a boolean value. Newer versions of jquery (1.6+) have a new method called .prop
which returns the boolean property value, while .attr()
now properly returns the string value. I'm not sure if the attribute is always updated when the property changes.
It's a string, but jQuery lets you set it with a boolean value, which then changes the attribute accordingly. Setting it to "false" results in attr('checked')
returning undefined
.
There is a fundamental way of finding out the data types
console.log(typeof $('#AcceptAgreement').attr('checked'));
But before jQuery 1.7, it used to return property value, now it returns pure string.
Another alternative to this is .prop('checked')
which return boolean.
To write you must use:
$('#AcceptAgreement').attr('checked', 'checked');
If you wanna know if it is checked can use:
if($('#AcceptAgreement').attr('checked'))
you can use:
if ($('#AcceptAgreement').is(':checked'))
{
//...
}
or shortend:
$('#isAgeSelected').is(':checked') ? /*true*/ : /*false*/ ;
本文标签: javascriptWhat Data Type is (39checkbox39)attr(39checked39)Stack Overflow
版权声明:本文标题:javascript - What Data Type is $('#checkbox').attr('checked') - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745628261a2667109.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论