jquery Action check box (checkbox) 12 tips.
1. Get a single checkbox selection (three ways to type)
$ ("input:checkbox:checked"). Val ()
Or
$ ("input:[type= ' checkbox ']:checked"). Val ();
Or
$ ("input:[name= ' ck ']:checked"). Val ();
2. Get multiple checkbox selected items
$ (' Input:checkbox '). each (the function () {
if ($ (this). attr (' checked ') ==true) {
alert ($ (this). Val ());
}
);
3. Set the first checkbox to the selected value
$ (' Input:checkbox:first '). attr ("Checked", ' checked ');
Or
$ (' Input:checkbox '). EQ (0). attr ("Checked", ' true ');
4. Set the last checkbox to a selected value
$ (' Input:radio:last '). attr (' checked ', ' checked ');
Or
$ (' Input:radio:last '). attr (' checked ', ' true ');
5. Set any checkbox to the selected value according to the index value
$ (' Input:checkbox). EQ (index value). attr (' checked ', ' true ');
Index value =0,1,2 ....
Or
$ (' Input:radio '). Slice (1,2). attr (' checked ', ' true ');
6, select multiple checkbox and select both the 1th and 2nd checkbox
$ (' Input:radio '). Slice (0,2). attr (' checked ', ' true ');
7. Set checkbox to selected value according to value
$ ("input:checkbox[value= ' 1 ']"). attr (' checked ', ' true ');
8, delete the checkbox value=1
$ ("input:checkbox[value= ' 1 ']"). Remove ();
9, delete the first few checkbox
$ ("Input:checkbox"). EQ (index value). Remove ();
Index value =0,1,2 ....
If you delete a 3rd checkbox:
$ ("Input:checkbox"). EQ (2). Remove ();
10. Traverse checkbox
$ (' Input:checkbox '). each (function (index, domele) {
//write Code
});
11, All selected
$ (' Input:checkbox '). each (function () {
$ (this). attr (' checked ', true);
});
12, all cancel the selection
$ (' Input:checkbox '). each (function () {
$ (this). attr (' checked ', false);
});
Some related operations of jquery on a checkbox
First, select the checkbox by selector:
1. Set an id attribute to the checkbox and select it by ID selector:
<input type= "checkbox" Name= "Mybox" id= "Chkone" value= "1" checked= "checked"/>
Jquery:
$ ("#chkOne"). Click (function () {});
2. Set a class attribute to the checkbox, selected by the class selector:
<input type= "checkbox" Name= "Mybox" class= "Chktwo" value= "1" checked= "checked"/>
Jquery:
$ (". Chktwo"). Click (function () {});
3. Select by Tag Selector and property selector:
<input type= "checkbox" Name= "Somebox" value= "1" checked= "checked" "/> <input type="
checkbox "Name=" Somebox "value=" 2 "/>
Jquery:
$ ("Input[name= ' Somebox ']"). Click (function () {});
Ii. operation of the checkbox:
Take this checkbox code as an example:
<input type= "checkbox" name= "box" value= "0" checked= "checked"/> "<input" type= "
checkbox" name= "box" value= "1"/>
<input type= "checkbox" name= "box" value= "2"/> <input type= "
checkbox" name= "box" value= "3"/ >
1. Traverse checkbox with each () method:
$ ("Input[name= ' box ']"). each (function () {});
2. Set checkbox to be selected with attr () method:
$ ("Input[name= ' box ']"). attr ("Checked", "checked");
In HTML, if a check box is selected, the corresponding tag is checked= "checked". But if you use jquery alert ($ ("#id"). attr ("Checked"), you will be prompted to be "true" instead of "checked", so judge if ("Checked" ==$ ("#id"). attr ("checked") Is wrong, should be if (true = = $ ("#id"). attr ("checked")
3. Get the value of the selected checkbox:
$ ("Input[name= ' box '][checked]"). each (function () {
if (true = = $ (this). attr ("checked")) {
alert ($ (this). attr (' value ');
}
Or:
$ ("Input[name= ' box ']:checked"). each (function () {
if (true = = $ (this). attr ("checked")) {
alert ($ (this). attr ( ' value ');
}
$ ("Input[name= ' box ']:checked") and $ ("Input[name= ' box") "What's the difference?" I tried using $ ("Input[name= ' box") to succeed.
4. Get the value of the unchecked checkbox:
$ ("Input[name= ' box ']"). each (function () {
if ($ (this). attr (' checked ') ==false) {
alert ($ (this). Val ());
}
);
5. Set the value of the checkbox's Values property:
$ (this). attr ("value", values);
Third, the general is to create a JS array to store the value of the Traversal checkbox to create a JS array method:
1. var array= new Array ();
2. Add data to the array:
Array.push ($ (this). Val ());
3. Array with "," separate output:
Alert (Array.join (', '));