在 javascript 中,我有获取水果名称的指针。但警报总是显示为空。我检查了我的逻辑,但找不到问题。有什么建议么?谢谢!
<!DOCTYPE html>
<html>
<head>
<title>My Fruit</title>
<script type="text/javascript">
function checkFruit(){
var fruit_radio_pointers = document.querySelector('[name="fruit"]');
var which_fruit = null;
for(var i=0; i<fruit_radio_pointers.length; i++){
if(fruit_radio_pointers[i].checked){
which_fruit = fruit_radio_pointers[i].value;
break;
}
}
alert(which_fruit);
}
//document.getElementById("my_btn").addEventListener("click", checkFruit, false);
</script>
</head>
<body>
<p>
<input name="fruit" type="radio" value="apple" /> Apple<br />
<input name="fruit" type="radio" value="cherry" /> Cherry
</p>
<p>
<button id="my_btn" onclick="checkFruit()" >Which Fruit?</button>
</p>
</body>
</html>