------------------------------------------------------------------------------------------
For a small example, jquery implements the Select All/Cancel/reverse selection of a checkbox in a form
[Email protected] <www.chenwei.ws>---------------------------
<input type= "checkbox" class= "All"/>
<input type= "checkbox" Name= "id[]" value= "1"/>a
<input type= "checkbox" Name= "id[]" value= "2"/>b
<input type= "checkbox" Name= "id[]" value= "3"/>c
<button class= "Selectinverse" > Anti-selection </button>
<script>
$ ('. All '). Toggle (
function ({
$ (": input[name=id[]]"). each (function () {
$ (this). attr (' checked ', true);
});
}),
function ({
$ (": input[name=id[]]"). each (function () {
$ (this). attr (' checked ', false);
});
})
);
$ ('. Selectinverse '). Click (function () {
$ (": input[name=id[]]"). each (function () {
$ (this). attr (' checked ',!this.checked);
});
});
</script>
-----------------------------------------------------------------------------------------
The above uses the event: Click, event Toggle: Toggle, Form element match: input, set element properties: attr, array, and object manipulation: each;
$ (). each (); specifically used to traverse jquery objects;
$.each (object, [callback]) is a jquery universal traversal method that can be used to traverse objects and arrays;
Example:
1. Iterate through the array, using both the element index and the content
$.each ([0,1,2], function (i, n) {
Console.log (' item: ' + i + ', value: ' + n ');
});
2. Iterate over objects, using both member names and variable contents
$.each ({name: ' Chenwei ', Age: ' Bayi '}, function (i, n) {
Console.log (' name: ' + i + ', age: ' + n ');
});
-----------------------------------------------------------------------------------------