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:
- $ (": CheckBox"). attr ("Checked", true);
- $ ("option"). attr ("selected", true);
- $ ("input"). attr ("ReadOnly", true);
- $ ("input"). attr ("Disabled", true);
Even this kind of code:
- 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