最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get value of thechecked radio - Stack Overflow

programmeradmin5浏览0评论

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" />&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>

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.

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" />&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>

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.

Share Improve this question edited Dec 1, 2011 at 20:00 NoWar 37.8k84 gold badges339 silver badges515 bronze badges asked Apr 20, 2011 at 16:25 codeomnitrixcodeomnitrix 4,24919 gold badges68 silver badges103 bronze badges 2
  • Is int not a reserved keyword in js? – geekchic Commented Apr 20, 2011 at 16:31
  • @geekchic: Nope, see the spec: in, instanceof and perhaps interface are, but int is not. – Marcel Korpel Commented Apr 20, 2011 at 16:48
Add a ment  | 

5 Answers 5

Reset to default 3

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

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.

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

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.

See it yourself:

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

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

评论列表(0)

  1. 暂无评论