jquery attr prop checkbox already has checked=checked but does not show a check question

Source: Internet
Author: User
Recently, in the process of doing a project encountered such a problem: in the process of using the bootstrap modal box, there is a checkbox in the Modal box, initially selected, when the Trigger modal box button is clicked, when Chcekbox is selected, a tick is displayed, This time the selected state is cached, then click the Close button in the Modal box, click the Trigger Modal Box button again to pop up the modal box, this time the problem arises:

The checkbox tag already has checked=checked but does not show tick, view Picture:

There is also a solution on the web, that is, the $ ("..."). attr ("Checked", true) is changed to $ ("..."). Prop ("checked", true), problem solved, but the problem is solved, then the reason is where it appears.

First, to understand the attr () function and the prop () function in the next Jquey, attr () is the value of the attribute, and prop () is the value of the property, before JQuery 1.6. The attr () method takes some attribute Value, the value of the property is returned, which results in inconsistencies. Starting with JQuery 1.6, the. Prop () method method returns the value of the property, and the. attr () method returns the value of attributes, which in the final analysis is the attribute and property.

Many attribute nodes have a corresponding property attribute, so attribute and properties can easily be confused, such as the ID and class in a DIV element are both attribute and Either way can be accessed and modified, but for the custom attribute node, or custom property, the two are not related (but in ie6-7, the two are the same, good wonderful, I hope we can all soon abandon IE8 below), need to note that For the checked attribute (attribute) does not correspond to its Checked property, attribute actually corresponds to the defaultchecked attribute and is used only to set the initial value of the check box. The attribute of the checked will not change due to the state of the check box, and checked's property will change due to the state of the check box, so in the. attr () function, even if it is set to. attr ("Checked", true), It is also used only to store default values for default or checked properties, but does not change the check box to be selected and selected, which is why the checkbox label already has checked=checked but does not display the tick.


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.