判斷checkbox是否被選中,判定選中的個數!


一般,我們對前台的checkbox進行處理。

要考慮一是,單個checkbox的處理

這個就比較簡單了,

例如

<input type="checkbox" id="ck" onclick="coutck();" value="1">1

我們來用js進行處理

<script type="text/javascript">

function coutck(){

if(document.getElementsById(ck).checked){

alert(document.getElementsById(ck).value);

}

}

</script>

如果checkbox是個數組,而且我們要判斷checkbox選中的個數的時候,我們可以進行如下處理

<script type="text/javascript"> function form1(){ var x=document.getElementsByName("ke[]"); var m=0; var n=false; for(var i=0;i<x.length;i++){ if(x[i].checked){ n=true; m++;
}
} if(!n){ alert("至少選擇一個啊"); } if(m>5){ alert("選這么多干嗎"); }
 } </script> <body> <form action="#" method="POST" onsubmit="return form1();"> <div> <input type="checkbox" id="kee" name="ke[]" value="1">11111 <input type="checkbox" id="kee" name="ke[]" value="2">22222 <input type="checkbox" id="kee" name="ke[]" value="3">33333 <input type="checkbox" id="kee" name="ke[]" value="4">44444 <input type="checkbox" id="kee" name="ke[]" value="5">55555 <input type="checkbox" id="kee" name="ke[]" value="6">66666 <input type="checkbox" id="kee" name="ke[]" value="7">77777 <input type="checkbox" id="kee" name="ke[]" value="8">88888 <input type="checkbox" id="kee" name="ke[]" value="9">99999 </div> <div><input type="submit" value="發布"></div> </form>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM