Using JavaScript to make a set of checkpoints on the page select/cancel all, the logic is very simple, and the implementation of code is not too difficult syntax. However, jquery is simpler to implement and the code is concise and incisive!
Jquery version: 1.3.2
Code
<HTML>
<Head>
<SCRIPT src = "jquery-1.3.2.min.js" type = "text/JavaScript"> </SCRIPT>
</Head>
<Body>
<Input type = "checkbox" name = "chk_list" id = "chk_list_1" value = "1"/> 1 <br/>
<Input type = "checkbox" name = "chk_list" id = "chk_list_2" value = "2"/> 2 <br/>
<Input type = "checkbox" name = "chk_list" id = "chk_list_3" value = "3"/> 3 <br/>
<Input type = "checkbox" name = "chk_list" id = "chk_list_4" value = "4"/> 4 <br/>
<Input type = "checkbox" name = "chk_all" id = "chk_all"/> select all/cancel all
<SCRIPT type = "text/JavaScript">
$ ("# Chk_all"). Click (function (){
$ ("Input [name = 'chk _ list']"). ATTR ("checked", $ (this). ATTR ("checked "));
});
</SCRIPT>
</Body>
</Html>
Jquery. ATTR gets/Sets object attribute values, such:
$ ("Input [name = 'chk _ list']"). ATTR ("checked"); // read the status of all objects whose names are 'chk _ List' (whether or not selected)
$ ("Input [name = 'chk _ list']"). ATTR ("checked", true); // set the checked value of all objects whose names are 'chk _ list' to true.
Another example is:
$ ("# Img_1"). ATTR ("src", "test.jpg"); // set the value of srcto 'test.jpg 'with the ID of img_1'
$ ("# Img_1"). ATTR ("src"); // read the SRC value whose ID is img_1
The following code gets the value of the checkbox selected in the above instance:
Code
<SCRIPT type = "text/JavaScript">
// Obtain all the checkboxes (sets) whose names are 'chk _ list)
VaR arrchk = $ ("input [name = 'chk _ list] [checked]");
// Obtain the value of each checkbox through Traversal
For (VAR I = 0; I <arrchk. length; I ++)
{
Alert (arrchk [I]. value );
}
</SCRIPT>
I am very grateful to anyone who can write the above traversal process using $. Each.
Thank youRainnolessThe following code traverses with $. Each:
<SCRIPT type = "text/JavaScript">
VaR arrchk = $ ("input [name = 'chk _ list'] [checked]");
$ (Arrchk). Each (function (){
Window. Alert (this. value );
});
});
</SCRIPT>