- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <title>js實現checkbox全選,反選,全不選</title>
- <script type="text/javascript">
- //複選框全選
- function checkAll(formvalue) {
- var roomids = document.getElementsByName(formvalue);
- for (var j = 0; j < roomids.length; j++) {
- if (roomids.item(j).checked == false) {
- roomids.item(j).checked = true;
- }
- }
- }
-
- //複選框全不選
- function uncheckAll(formvalue) {
- var roomids = document.getElementsByName(formvalue);
- for (var j = 0; j < roomids.length; j++) {
- if (roomids.item(j).checked == true) {
- roomids.item(j).checked = false;
- }
- }
- }
-
- //複選框選擇轉換
- function switchAll(formvalue) {
- var roomids = document.getElementsByName(formvalue);
- for (var j = 0; j < roomids.length; j++) {
- roomids.item(j).checked = !roomids.item(j).checked;
- }
- }
-
- </script>
- </head>
- <body>
- <input type="radio" name="all" id="all" onclick="checkAll('test')" />
- 全選
- <input type="radio" name="all" id="Checkbox1" onclick="uncheckAll('test')" />
- 全不選
- <input type="radio" name="all" id="Checkbox2" onclick="switchAll('test')" />
- 反選<br />
- <sel
- <input name="test" value="複選框1" type="checkbox" />複選框1<br />
- <input name="test" value="複選框2" type="checkbox" />複選框2<br />
- <input name="test" value="複選框3" type="checkbox" />複選框3<br />
- <input name="test" value="複選框4" type="checkbox" />複選框4<br />
- <input name="test" value="複選框5" type="checkbox" />複選框5<br />
- <input name="test" value="複選框6" type="checkbox" />複選框6<br />
- </body>
- </html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>js實現checkbox全選,反選,全不選</title> <script type="text/javascript"> //複選框全選 function checkAll(formvalue) { var roomids = document.getElementsByName(formvalue); for (var j = 0; j < roomids.length; j++) { if (roomids.item(j).checked == false) { roomids.item(j).checked = true; } } } //複選框全不選 function uncheckAll(formvalue) { var roomids = document.getElementsByName(formvalue); for (var j = 0; j < roomids.length; j++) { if (roomids.item(j).checked == true) { roomids.item(j).checked = false; } } } //複選框選擇轉換 function switchAll(formvalue) { var roomids = document.getElementsByName(formvalue); for (var j = 0; j < roomids.length; j++) { roomids.item(j).checked = !roomids.item(j).checked; } } </script></head><body> <input type="radio" name="all" id="all" onclick="checkAll('test')" /> 全選 <input type="radio" name="all" id="Checkbox1" onclick="uncheckAll('test')" /> 全不選 <input type="radio" name="all" id="Checkbox2" onclick="switchAll('test')" /> 反選<br /> <sel <input name="test" value="複選框1" type="checkbox" />複選框1<br /> <input name="test" value="複選框2" type="checkbox" />複選框2<br /> <input name="test" value="複選框3" type="checkbox" />複選框3<br /> <input name="test" value="複選框4" type="checkbox" />複選框4<br /> <input name="test" value="複選框5" type="checkbox" />複選框5<br /> <input name="test" value="複選框6" type="checkbox" />複選框6<br /></body></html>
原文網址:http://xdwangiflytek.iteye.com/blog/1343416