jquery Determines whether the checkbox is returned with checked or undefined

Source: Internet
Author: User
Tags tagname

$ (' #checkbox '). attr (' checked '); The return is checked or undefined, not the original true and false, and the workaround for this problem is as follows


In the previous version of JQ1.6, we would write our code like this:

<input type= ' checkbox ' id= ' CB '/><script>//gets whether var isChecked = $ (' #cb ') is selected. attr (' checked ');   Set check $ (' #cb '). attr (' checked ', true); </script>


> This is not a problem before JQ1.6, but when we upgrade JQ1.6 to a higher version, the problem comes when we find:  
$ (' #cb '). attr (' checked '); The return is checked or undefined, not the original true and false.   and the Checked property is initialized when the page is initialized, and does not change as the state changes. So if the checkbox is initially selected, the return is checked, and if it is not selected at first, then undefined

is returned.


analysis of the reasons, it can be understood:  
It distinguishes between "attribute" and "property", which refers to "name,id" and so on, which refers to "SelectedIndex, TagName, NodeName" and so on.
After JQ1.6, the attr method can be used to obtain the properties, and the prop method to obtain the characteristics

$ ("#cb"). attr ("TagName"); Undefined $ ("#cb"). Prop ("TagName"); INPUT




jquery Determines whether the checkbox is returned with checked or undefined

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.