最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What Data Type is $('#checkbox').attr('checked') - Stack Overflow

programmeradmin2浏览0评论

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
Add a ment  | 

6 Answers 6

Reset to default 4

Probably 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*/ ;
发布评论

评论列表(0)

  1. 暂无评论