獲取單選框的值

 
<html>
<head>
<title>獲取單選框的值</title>
<script language="javascript" type="text/javascript">
function test()
{

       var commend 
= theForm.commend.value;

       
for(var i=0;i<theForm.commend.length;i++){
           
if(theForm.commend[i].checked==true){
              commend 
= theForm.commend[i].value;
              
break;
           }

       }


       alert(
"單選框的值爲:"+commend);
}

</script>
</head>
<body>
<form name="form" onsubmit="test()">
       
<input type="radio" name="commend" value="1">正確
       
<input type="radio" name="commend" value="0">錯誤
       
<input type="submit" >
</form>
</body>
</html>

如果是已知的單選框的順序,那就可以直接用radio[i].value來獲取它的值,而不用循環了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章