Bytes ------------------------------------------------------------------------------------------
In this example, jQuery selects, cancels, and reselect all the checkboxes in the form.
------------------------- @ Chenwei <www. chenwei. ws> ---------------------------
<Input type = "checkbox" class = "all"/>
<Input type = "checkbox" name = "id []" value = "1"/>
<Input type = "checkbox" name = "id []" value = "2"/> B
<Input type = "checkbox" name = "id []" value = "3"/> c
<Button class = "selectInverse"> invert 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>
Bytes -----------------------------------------------------------------------------------------
The above event is used: click, event switching: toggle, form Element matching: input, set the attribute of the element: attr, array and object operation: each;
$ (). Each (); used to traverse jQuery objects;
$. Each (object, [callback]) is a universal jQuery Traversal method that can be used to traverse objects and arrays;
Example:
1. traverse the array and use the element index and content at the same time.
$. Each ([0, 1, 2], function (I, n ){
Console. log ('item: '+ I +', value: '+ n );
});
2. traverse the object and use both the member name and variable content
$. Each ({name: 'chenwei', age: '81 '}, function (I, n ){
Console. log ('name: '+ I +', age: '+ n );
});
Bytes -----------------------------------------------------------------------------------------