最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin8PV0评论

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*/ ;
Post a comment

comment list (0)

  1. No comments so far