JQuery CheckBox (check box) operation method summary, jquerycheckbox
1. Obtain the selected items of a single checkbox (three methods ):
Copy codeThe Code is as follows:
$ ("Input: checkbox: checked"). val ()
Or
Copy codeThe Code is as follows:
$ ("Input: [type = 'checkbox']: checked"). val ();
Or
Copy codeThe Code is as follows:
$ ("Input: [name = 'ck ']: checked"). val ();
2. Obtain multiple checkbox selected items:
Copy codeThe Code is as follows:
$ ('Input: checkbox'). each (function (){
If ($ (this). attr ('checked') = true ){
Alert ($ (this). val ());
}
});
Or
Copy codeThe Code is as follows:
('Input: checkbox'). map (function (){
Return (this). val ();
}). Get (). join (',');
3. Set the first checkbox as the selected value:
Copy codeThe Code is as follows:
$ ('Input: checkbox: first '). attr ("checked", 'checked ');
Or
Copy codeThe Code is as follows:
$ ('Input: checkbox'). eq (0). attr ("checked", 'true ');
4. Set the last checkbox as the selected value:
Copy codeThe Code is as follows:
$ ('Input: radio: la'). attr ('checked', 'checked ');
Or
Copy codeThe Code is as follows:
$ ('Input: radio: la'). attr ('checked', 'true ');
5. set any checkbox as the selected value based on the index value:
Copy codeThe Code is as follows:
$ ('Input: checkbox). eq (index value). attr ('checked', 'true'); index value = 0, 1, 2 ....
Or
Copy codeThe Code is as follows:
$ ('Input: radio '). slice (1, 2). attr ('checked', 'true ');
6. Select multiple checkboxes:
Select 1st and 2nd checkboxes at the same time:
Copy codeThe Code is as follows:
$ ('Input: radio '). slice (). attr ('checked', 'true ');
7. Set checkbox as the selected Value based on the Value:
Copy codeThe Code is as follows:
$ ("Input: checkbox [value = '1']"). attr ('checked', 'true ');
8. Delete the checkbox with Value = 1:
Copy codeThe Code is as follows:
$ ("Input: checkbox [value = '1']"). remove ();
9. Delete the following checkboxes:
Copy codeThe Code is as follows:
$ ("Input: checkbox"). eq (index value). remove (); index value = 0, 1, 2 ....
For example, delete 3rd checkboxes:
Copy codeThe Code is as follows:
$ ("Input: checkbox"). eq (2). remove ();
10. traverse the checkbox:
Copy codeThe Code is as follows:
$ ('Input: checkbox'). each (function (index, domEle ){
// Write code
});
11. Select All
Copy codeThe Code is as follows:
$ ('Input: checkbox'). each (function (){
$ (This). attr ('checked', true );
});
12. deselect all options:
Copy codeThe Code is as follows:
$ ('Input: checkbox'). each (function (){
$ (This). attr ('checked', false );
});