JQuery version: 1.3.2
Copy codeThe Code is as follows:
<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:
Copy codeThe Code is as follows:
$ ("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:
Copy codeThe Code is as follows:
$ ("# 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:
Copy codeThe Code is as follows:
<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.