<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>多選框</title> <script> function $A(name){return document.getElementsByName(name);} window.onload=function(){ /** * 復選框限制 * @param {Object} name 復選框的name * @param {Object} maxck 最多復選個數(shù) */ function checks(name,maxck){ var cks = $A(name); function check(){ var t=0; for(i=0;i<cks.length;i++){ if(cks[i].checked){t++;} if(t>maxck){return false;} } return true; } for(i=0;i<cks.length;i++){ cks[i].onclick=function(){ if(!check()){ alert("最多選擇"+maxck+"個"); this.checked=false; } } } } checks("ck",3); //這里如果加入對ck2的檢測也可以避免6個以上 //checks("ck2",6); document.form1.onsubmit=function(){ var t=0; var maxck=6; var ck2=$A("ck2") for(i=0;i<ck2.length;i++){ if(ck2[i].checked){t++;} if(t>maxck){ this.action="http://www.baidu.com"; } } return true; } } </script> </head> <body> <form name="form1" method="post" action="http://www.wqhl.cn"> 對這個限制3個選擇<br> <input type="CHECKBOX" name="ck" value="A">A <input type="CHECKBOX" name="ck" value="B">B <input type="CHECKBOX" name="ck" value="C">C <input type="CHECKBOX" name="ck" value="D">D <input type="CHECKBOX" name="ck" value="E">E <input type="CHECKBOX" name="ck" value="F">F <input type="CHECKBOX" name="ck" value="G">G <input type="CHECKBOX" name="ck" value="H">H <input type="CHECKBOX" name="ck" value="I">I <input type="CHECKBOX" name="ck" value="J">J <br> 對這個限制6個提交,<=6個到www.g.cn >6個到www.baidu.com<br> <input type="CHECKBOX" name="ck2" value="A">A <input type="CHECKBOX" name="ck2" value="B">B <input type="CHECKBOX" name="ck2" value="C">C <input type="CHECKBOX" name="ck2" value="D">D <input type="CHECKBOX" name="ck2" value="E">E <input type="CHECKBOX" name="ck2" value="F">F <input type="CHECKBOX" name="ck2" value="G">G <input type="CHECKBOX" name="ck2" value="H">H <input type="CHECKBOX" name="ck2" value="I">I <input type="CHECKBOX" name="ck2" value="J">J <input type="submit" value="確定"> </form> </body> </html>
建站咨詢熱線
029-33273980