JavaScript-事件響應--複選框全選
阿新 • • 發佈:2020-10-27
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <script language="JavaScript" type="text/JavaScript"> function doSelectItem (f) {var testform = document.getElementById("MyForm"); if (f ==0) {for(var i =0 ;i<testform.elements.length;i++) {if (testform.elements[i].type=="checkbox") {testform.elements[i].checked=true;} } }else {if (f == 1) {for (var i =0;i<testform.elements.length;i++) {if (testform.elements[i].type=="checkbox") {testform.elements[i].checked=false;} } } }else {if (f == 2) {for (var i =0;i<testform.elements.length;i++) {if (testform.elements[i].type =="checkbox") {testform.elements[i].checked= !testform.elements[i].checked;} } } }} </script> </head> <body> <center> <form action="HTMLPage37.html" method="get" id="MyForm" name="MyForm"> <input type="checkbox" name="hobi" />體育 <input type="checkbox" name="hobi" />讀書 <input type="checkbox" name="hobi" />旅遊 <input type="checkbox" name="hobi" />音樂<br> <input type="button" onclick="doSelectItem(0);" value="全選"> <input type="button" onclick="doSelectItem(1);" value="清空"> <input type="button" onclick="doSelectItem(2);" value="反選"> </form> </center> </body> </html>