如何获取已检查收音机的价值

发布于 2024-11-02 16:24:58 字数 2157 浏览 1 评论 0原文

我试图使用以下代码获取选中的单选按钮的值:

<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" />&nbsp; &nbsp;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" />&nbsp; &nbsp;Operating System is used for Efficeint Resource Sharing <br/>
             <input type="radio" name="options" value="optC" />&nbsp; &nbsp;Operating System is used for Efficeint Resource Sharing <br/>
             <input type="radio" name="options" value="optD" />&nbsp; &nbsp;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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

满地尘埃落定 2024-11-09 16:24:58

也许您的意思是 document.getElementsByName()。请注意复数,因为它可能返回多个节点。

Perhaps you mean document.getElementsByName(). Please notice the plural, since it can possibly return more than one node.

拥有 2024-11-09 16:24:58

问题可能是您正在调用“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.

無處可尋 2024-11-09 16:24:58

我相信该方法是复数 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 use document.getElementById or use name and narrow down your array.

javascript getElementByName doesn't work

意犹 2024-11-09 16:24:58

只需更改此行:

var allAnchors = document.getElementsByName("options");

改为:

var allAnchors = document.getElementsByTagName("a");

您的代码应该按预期工作

getElementsByName 有其自己的用法,但要获取文档中的所有锚标记,您需要其他方法,即 getElementsByTagName

Just change this line:

var allAnchors = document.getElementsByName("options");

To this instead:

var allAnchors = document.getElementsByTagName("a");

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 is getElementsByTagName.

聚集的泪 2024-11-09 16:24:58

自己看吧:

javascript:a=document.getElementsByName("options")[0];alert(a.value)

javascript:a=document.getElementsByTagName("input")[0];alert(a.value)

See it yourself:

javascript:a=document.getElementsByName("options")[0];alert(a.value)

javascript:a=document.getElementsByTagName("input")[0];alert(a.value)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文