我正在尝试使用以下代码获取选中的单选按钮的值:
<html>
<head>
<script type="text/javascript" >
window.onload = initAll;
function initAll(){
var allAnchors = document.getElementsByName("options");
for ( var int = 0; int < allAnchors.length; int++) {
if(allAnchors[int].className == "buttonLink"){
allAnchors[int].onclick = fetchQuestion;
}
}
}
function fetchQuestion(){
var toLoad = this.href;
var selectedAnswer = "";
var allRadio = document.getElementsByTagName("input");
for(var i = 0;i<allRadio.length; i++){
if(allRadio[i].checked == true){
selectedAnswer = allRadio[i].value;
}
}
alert(selectedAnswer);
return false;
}
</script>
</head>
<body>
<input type="radio" name="options" value="optA" /> Operating System is used for Efficeint Resource Sharing Operating System is used for Efficeint Resource Sharing Operating System is used for Efficeint Resource Sharing <br/>
<input type="radio" name="options" value="optB" /> Operating System is used for Efficeint Resource Sharing <br/>
<input type="radio" name="options" value="optC" /> Operating System is used for Efficeint Resource Sharing <br/>
<input type="radio" name="options" value="optD" /> Operating System is used for Efficeint Resource Sharing <br/>
<a href="SubmitSheet" class="buttonLink">Submit</a>
<a href="NextQuestion" class="buttonLink">Next</a>
<a href="PreviousQuestion" class="buttonLink">Previous</a>
<a href="PassQuestion" class="buttonLink">Pass</a>
</body>
</html>
如果我在这里使用document.getElementByTagName("input")
,那么它正在工作。但document.getElementByName("options")
事实并非如此。那么这有什么问题呢??我不能将 document.getElementByName 与单选按钮一起使用。