获取checkbox的值,实例如下:

<html>
    <form name=\"orderForm\">
      <input type=\"checkbox\" name=\"onion\" value=\"hot onion\">Onion<br>
      <input type=\"checkbox\" name=\"bacon\" value=\"spicy bacon\">Bacon<hr>
      <input type=\"button\" value=\"Order Pizza\" name=\"orderButton\"  =\"console.log(\'ordered.\')\">
    </form>
    <  language=\" \">
    document.write(document.orderForm.onion.value);   
    document.write(\" or \",document.orderForm.bacon.value);  
    </ >
</html>

 

再来看一个实例,该实例获取所有被选中的checkbox值:

<html>
<head>
<  http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
< >js</ >
</head>
<  language=\" \">
function aa(){
    var r=document.getElementsByName(\"r\"); 
    for(var i=0;i<r.length;i++){
         if(r[i].checked){
         alert(r[i].value+\",\"+r[i].nextSibling.nodeValue);
       }
    }      
}
</ >
<body>
<form name=\"form1\" method=\"post\" action=\"\">
<input type=\"checkbox\" name=\"r\" value=\"1\">a<br>
<input type=\"checkbox\" name=\"r\" value=\"2\">b<br>
<input type=\"checkbox\" name=\"r\" value=\"3\">c<br>
<input type=\"checkbox\" name=\"r\" value=\"4\">d<br>
<input type=\"checkbox\" name=\"r\" value=\"5\">e<br>
<input type=\"checkbox\" name=\"r\" value=\"6\">f<br>
<input type=\"checkbox\" name=\"r\" value=\"7\">g<br>
<input type=\"checkbox\" name=\"r\" value=\"8\">h<br>
<input type=\"checkbox\" name=\"r\" value=\"9\">i<br>
<input type=\"checkbox\" name=\"r\" value=\"10\">j<br>
<br>
<input type=\"button\"  =\"aa()\" value=\"button\">
</form>
</body>
</html>
收藏 打印