Use jquery to select all or cancel all checkbox

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.