在html中,頁面只有這些代碼
<script type="text/javascript">
function ok(){
//獲取所有單選按鈕(同一組),得到對象
var fruits = document.getElementByName("fruit");
for(var i=0;i<fruits.length;i++){
if(fruits[i].checked){
alert("選擇了"+fruits[i].value);
}
}
}
</script>
<body>
<div>
<input type="radio" name ="fruit" value="apple" onclick="ok()">蘋果
<input type="radio" name ="fruit" value="banana" onclick="ok()" >香蕉
<input type="radio" name ="fruit" value="pear" onclick="ok()">梨
</div>
</body>
頁面顯示情況是這樣的:
選中時,情況是這樣的: