/***** 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) } } );