Select All/cancel all

Source: Internet
Author: User
/***** Select all, cancel all **/function checkall () {var checkboxlist = $ ('input [type = "checkbox"] ', document. forms [0]); var checksels = document. getelementbyid ("checksels"); checkboxlist. each (function (I) {If (! $ (This). ATTR ('Disabled ') This. Checked = checksels. Checked ;});}

 

 

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!

 

<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:

<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 (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>

 

From: http://www.cnblogs.com/bynet/archive/2009/11/13/1602491.html

 

 

 

 

Using jquery to select all and cancel all is not as complicated as JS and supports more browsers.

<MCE: Script Type = "text/JavaScript"> <! -- $ (Function () {$ ("# checkall "). click (function () {$ ("input [@ name = 'checkname [] ']"). each (function () {$ (this ). ATTR ("checked", true) ;}) ;}$ ("# delcheckall "). click (function () {$ ("input [@ name = 'checkname [] ']"). each (function () {$ (this ). ATTR ("checked", false) ;}) ;};}); // --> </MCE: SCRIPT> <input type = 'checkbox' id = 'id1' name = 'checkname [] 'value = '1'/> value1 <input type = 'checkbox' id = 'id2 'name = 'checkname [] 'value = '2'/> value2 <input type = 'checkbox' id = 'id3 'name = 'checkname [] 'value = '3' /> value3 <input type = "button" id = "checkall" name = "checkall" value = "select all"/> <input type = "button" id = "delcheckall" Name = "delcheckall" value = "cancel all selections"/>

A simpler one: a simpler one:

$("#checkall").click(   function(){     if(this.checked){         $("input[name='checkname']").attr('checked', true)    }else{         $("input[name='checkname']").attr('checked', false)    }   } );

 

 

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.