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*/ ;
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745628261a4636944.html
评论列表(0条)