We all know that JQuery can set the DOM properties by the Attr method, and naturally think of the $ (DOM). attr (' checked ', ' checked '); This method selects the checkbox, but there is a problem with the recent use of checkbox selection and reverse selection in the project:
After using the Attr method to set the selection, click the first time to set the reverse, but not the second time.
$ (function () {
var checkbox = $ ("input[type= ' checkbox ']");
Select All
$ (' #select-all '). Click (function () {
Checkbox.attr (' checked ', true);
});
Anti-election
$ (' #select-reverse '). Click (function () {
Checkbox.each (function (i, DOM) {
if ($ (DOM). attr (' checked ')) {
$ (DOM). Removeattr (' checked ');
} else {
$ (DOM). attr (' checked ', ' checked ');
}
});
});
});
After looking at some material, only to discover: for jquery 1.6.0+ version, want to use prop method to set.
$ (function () {
var checkbox = $ ("input[type= ' checkbox ']");
//Select All
& nbsp; $ (' #select-all '). Click (function () {
Checkbox.prop ("Checked", true);
});
//anti-select
$ (' #select-reverse '). Click (function () {
checkbox.prop ("Checked", function (index, attr) {
return!attr;
});
});
});