Jquery cannot set the checkbox to be selected, that is, it does not change to the selected status _ jquery

Source: Internet
Author: User
After the settings, the checkbox does not change to the selected status. After debugging with chrome, you can check that the checkbox does have the checked attribute. To solve this problem, you can refer to the next article. The Code is as follows:


$ ("Input"). attr ("checked", "checked ")


After setting, the checkbox does not change to the selected status. After debugging with chrome, you can see that the checkbox does have the checked attribute and the value is checked, but the page is still displayed as unselected.

The Code is as follows:


$ ("Input"). prop ("checked", true );



The differences between ttributes and properties are important in specific situations. Before jQuery 1.6, the. attr () method will return the property value when obtaining the value of some attributes, which leads to inconsistent results. Starting from jQuery 1.6, the. prop () method returns the value of property, while the. attr () method returns the value of attributes.

For example, selectedIndex, tagName, nodeName, nodeType, ownerDocument, defaultChecked, and defaultSelected should use the. prop () method for value or value assignment. Before jQuery1.6, these attributes were obtained using the. attr () method, but this is not the attr attribute of the element. They have no corresponding attributes (attributes), but only properties ).

For example, consider And assume it is a JavaScript variable named elem:

Elem. checked true (Boolean) will change the status of the check box
$ (Elem). prop ("checked") true (Boolean) changes the status of the check box
Elem. getAttribute ("checked") "checked" (String) does not change the initial status of the check box;
$ (Elem). attr ("checked") (1.6) "checked" (String) does not change the initial status of the check box;
$ (Elem). attr ("checked") (1.6.1 +) "checked" (String) changes the status of the check box
$ (Elem). attr ("checked") (pre-1.6) true (Boolean) will change the status of the check box
According to W3C form specifications, the checked attribute is a Boolean attribute, which means that the property corresponding to this attribute is true even if it does not have a value or an empty string. We recommend that you use a browser-compatible method to determine whether the checked attribute of the checkbox element is "true:

The Code is as follows:


If (elem. checked)
If ($ (elem). prop ("checked "))
If ($ (elem). is (": checked "))


If you use jQuery 1.6, the Code if ($ (elem). attr ("checked") will get an attribute, which does not change whether the check box is selected or selected. It is only used to store the initial values of default or selected attributes. To maintain backward compatibility ,. the attr () method starts from jQuery 1.6.1 +. Besides returning attribute values, it also updates the property attributes. Therefore, the boolean attribute (boolean attribute) does not need to pass. prop () to change its value. We recommend that you use one of the above methods to obtain the checked value.
Related Article

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.