如何获取已检查收音机的价值
我试图使用以下代码获取选中的单选按钮的值:
<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 与单选按钮一起使用吗?
I am trying to get the value of the checked radio button with the following code:
<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>
here if i used document.getElementByTagName("input")
, then it is working. but with document.getElementByName("options")
it is not. So what's the problem with this?? Can't i use document.getElementByName with radio buttons.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
也许您的意思是 document.getElementsByName()。请注意复数,因为它可能返回多个节点。
Perhaps you mean document.getElementsByName(). Please notice the plural, since it can possibly return more than one node.
问题可能是您正在调用“document.getElementByName("options")”
getElementByName 未定义
,您想要调用 getElementsByName,它将返回一个元素集合。
The problem is likely that you are calling "document.getElementByName("options")"
getElementByName is undefined
you want to call getElementsByName, which will return an element collection.
我相信该方法是复数
document.getElementsByName
因为许多项目可以共享一个名称。如果您想要单个元素,请添加一个 id 并使用document.getElementById
或使用 name 并缩小数组范围。javascript getElementByName 不起作用
I believe the method is plural
document.getElementsByName
because many items can share a name. If you want a single element, add an id and usedocument.getElementById
or use name and narrow down your array.javascript getElementByName doesn't work
只需更改此行:
改为:
您的代码应该按预期工作。
getElementsByName
有其自己的用法,但要获取文档中的所有锚标记,您需要其他方法,即getElementsByTagName
。Just change this line:
To this instead:
And your code should work as expected.
The
getElementsByName
has its own usage, but to grab all the anchor tags in the document you need other method which isgetElementsByTagName
.自己看吧:
See it yourself: