Jquery cannot set the checkbox to be selected.

Source: Internet
Author: User

Copy codeThe 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.
Copy codeThe 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 the <input type = "checkbox" checked = "checked"/> defined in the HTML Tag of a DOM element, and assume that it is an elem named by a JavaScript variable:

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:
Copy codeThe 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.