[JavaScript] select all or invert check boxes, determine which check boxes are selected, and select all javascript check boxes.
This function is not difficult to select and select all check boxes and determine which check boxes are selected,
The use of document. getElementsByName ("xxx"); can be easily implemented. Note that the returned value is a node array.
However, this feature is very considerate to users. The following example illustrates the problem.
The first is the following layout:
The HTML code is as follows, which is very simple. Three buttons are used to set the javascript functions corresponding to the onclick event.
Then there are four check boxes. Pay attention to setting the unified name value to form a node array. So that it can be obtained by the subsequent document. getElementsByName method.
<! Doctype html public "-// W3C // dtd html 4.01 // EN" "http://www.w3.org/TR/html4/strict.dtd"> The following is a javascript processing function. To achieve the following results, the processing script is as follows:
<Script type = "text/javascript"> // Where is the check box selected? Traverse the check box array and use value to retrieve its value function selected () {var checkBoxArr = document. getElementsByName ("checkBoxGroup"); var str = "the selected check boxes are:"; for (var I = 0; I <checkBoxArr. length; I ++) {if (checkBoxArr [I]. checked) {str + = checkBoxArr [I]. value + "," ;}} alert (str) ;}// select all. traverse the check box array. All checked values are true function selectAll () {var checkBoxArr = document. getElementsByName ("checkBoxGroup"); for (var I = 0; I <checkBoxArr. lengt H; I ++) {checkBoxArr [I]. checked = "true" ;}/// returns the check box array. All checked values are reversed. Function selectReserve () {var checkBoxArr = document. getElementsByName ("checkBoxGroup"); for (var I = 0; I <checkBoxArr. length; I ++) {checkBoxArr [I]. checked = (! CheckBoxArr [I]. checked) ;}</script>