<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來獲取它的值,而不用循環了。