jquery Cannot set checkbox selection is not turned to a selected state

Source: Internet
Author: User

  Settings After the checkbox does not become a selected state, with the Chrome debugging looked at, the checkbox does have the checked attribute, for this problem, you can refer to the following article

The code is as follows: $ ("input"). attr ("Checked", "checked")     Settings The checkbox does not become selected and, with Chrome debugging, it does have a checked attribute in the checkbox , and the value is checked, but the page display is still unchecked   the code is as follows: $ ("input"). Prop ("checked", true);      The difference between ttributes and properties is important in certain situations. Before JQuery 1.6, the. attr () method returns the value of the property when it takes a value of some attribute, which results in inconsistency. Starting with JQuery 1.6, the. Prop () method returns the value of the property, whereas the. attr () method returns the value of attributes.     For example, SelectedIndex, TagName, NodeName, NodeType, Ownerdocument, defaultchecked, and defaultselected should be used. Prop () method to take a value or assign a value. These properties are obtained using the. attr () method before jQuery1.6, but this is not the attr attribute of the element. They have no corresponding attributes (attributes), only attributes.     For example, consider the <input type= "checkbox" checked= "checked" as defined in the HTML markup for a DOM element, and assume that it is a JavaScript variable named/>: & nbsp   Elem.checked True (Boolean) changes the state of the check box   $ (elem). Prop ("checked") True (Boolean) changes the state of the check box   Elem.getattribute ("Checked") "Checked" (string) does not change the initial state of the check box;  $ (elem). attr ("Checked") (1.6) "checked" (string) The initial state of the checkbox that will not change;  $ (elem). attr ("Checked") (1.6.1+) "Checked" (String) changes the status of the check box   $ (elem). attr ("Checked") (pre-1.6) True (Boolean) changes the state of the check box   According to the form specification of the Consortium, The Checked property is a Boolean property, which means that as long as the attribute exists, even if it has no value, or an empty string, the property of the attribute is true. The following recommendations are compatible browsers to determine whether the checkbox element's Checked property is "true":    code as follows: if (elem.checked)   if ($ (elem). Prop ("Checked ")   if ($ (elem). Is (": Checked "))     If you use jquery 1.6, code if ($ (elem). attr (" Checked "), will get a property (attribute) , it does not change the check box to be selected and checked. It is simply used to store the initial value of the default or selected property. To maintain backward compatibility, the. attr () method starts with the JQuery 1.6.1+ and, in addition to returning property values, the property properties are updated, so the Boolean attribute (Boolean property) does not need to pass. Prop () to change its value. It is recommended to use one of the above methods to obtain the checked value.  

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.