jquery checkbox tick/Uncheck the odd problem that can only be manipulated once

Source: Internet
Author: User

First execution, no problem, but the second execution there is a problem, you can not choose

Workaround: Replace attr () with prop ()

$ ("#CheckedAll"). Click (function () {                if ($ (). Is (": Checked")) {                    $ ("[Name=items]:checkbox"). Prop (" Checked ", true);                } else {                    $ ("[Name=items]:checkbox"). Prop ("checked", false);                }            );

Ps:prop () and attr () differences:

(Source: http://hxq0506.iteye.com/blog/1046334)

Recently, in Iteye's news, jquery has been updated to 1.6.1. And the biggest change in previous versions is the addition of the. Prop method. But the. Prop () method and the. attr () method are literally difficult to distinguish. Both properties and attributes have the meaning of "attribute" in Chinese.
The following is a brief translation of the usage of. Prop () and. attr () According to this blog post (javascript:mctmp (0)):

1, upgrade from 1.5.2 to 1.6.1

By introducing the new method. Prop () and the change in the. attr () method, jQuery1.6.1 caused a heated discussion about the differences and linkages between attributes and properties. At the same time, 1.6.1 also resolves some backward compatibility issues. When upgrading from 1.5.2 to 1.6.1, you do not have to modify any attribute code.

The following is a description of the attributes module changes in jQuery1.6 and 1.6.1, as well as the preferred use of the. attr () method and the. Prop () method. However, as mentioned earlier, jQuery1.6.1 allows you to use the. attr () method just as it used to be in all cases.

2. What has changed

The change in the attributes module was to remove ambiguities between the attributes and properties, but it caused some confusion in the jquery community because one method was used in all versions prior to 1.6 (. attr ()) To deal with attributes and properties. But the old. attr () method has some bugs that are difficult to maintain. jQuery1.6.1 has updated the Attributes module and fixed several bugs.

In particular, the Boolean attributes, such as: checked,selected,readonly and disabled are treated the same as before 1.6.1 and 1.6. This means the following code:

    1. $ (": CheckBox"). attr ("Checked", true);
    2. $ ("option"). attr ("selected", true);
    3. $ ("input"). attr ("ReadOnly", true);
    4. $ ("input"). attr ("Disabled", true);

Even this kind of code:

    1. if ($ (": CheckBox"). attr ("checked")) {/ * do something */}

There is no need to make any changes in 1.6.1 in order to maintain the previously expected run results.

3. Overview of Preferred usage

The. Prop () method should be used to handle Boolean attributes/properties and properties that do not exist in HTML (for example, window.location). All other attributes (those you see in HTML) can and should continue to be manipulated using the. attr () method.

The above overview has been described clearly enough, and I do not need to summarize the

jquery checkbox tick/Uncheck the odd problem that can only be manipulated once

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.