简体   繁体   English

getElementsByName返回undefined

[英]getElementsByName returns undefined

This is my javascript: 这是我的javascript:

<script type="text/javascript">
    var crct_answrs = new Array(<?php echo implode(',', $crct_ans); ?>);
    var answrs = new Array();

    function check_ans(){
        for(var i = 1; i < 11; i++){
            var tst = "ques"+i;
            var radio = document.getElementsByName(tst);
            for (var x = 0; x < radio.length; x ++) {
                if (radio[x].checked) {
                    answrs = radio.value;
                }   
            }
        }       
    }   
</script>

This is part of HTML: 这是HTML的一部分:

<form>
            <div id="quest1" class="question">
                <div class="prblm">
                    <?php echo $questions[1]; ?></div>
                <table class="answrs">
                    <tr>
                        <td><?php echo $answrs[1][1]; ?></td>
                        <td><input name="ques1" type="radio" value="1"></td>
                    </tr>
                    <tr>
                        <td><?php echo $answrs[1][2]; ?></td>
                        <td><input name="ques1" type="radio" value="2"></td>
                    </tr>
                    <tr>
                        <td><?php echo $answrs[1][3]; ?></td>
                        <td><input name="ques1" type="radio" value="3"></td>
                    </tr>
                    <tr>
                        <td><?php echo $answrs[1][4]; ?></td>
                        <td><input name="ques1" type="radio" value="4"></td>
                    </tr>
                </table>
            </div>

I have 10 same pattern div tags with tables and the input name is change accordingly. 我有10个相同的模式div标签与表, input名称相应更改。 check_ans() runs on onclick . check_ans()onclick运行。 But all the time getElementsByName returns undefined. 但是getElementsByName返回undefined。 Why this happens. 为什么会这样。 Please help me. 请帮我。 Thanks! 谢谢!

Your error lies in: 你的错误在于:

if (radio[x].checked) {
    answrs = radio.value;
}

answrs is an array and needs to set values to the index of the corresponding iterator it's on. answrs是一个数组,需要将值设置为它所对应的迭代器的索引。 radio.value is trying to access value from the radio array, but has left out it's index as well. radio.value试图从radio阵列中访问值,但也忽略了它的索引。 Look at my solution below for the fix: 请查看下面的解决方案:


I've got a jsFiddle working. 我有一个jsFiddle工作。

JavaScript: JavaScript的:

var answrs = [];

function check_ans() {
    for (var i = 1; i <= 10; i++) {
        var radio = document.getElementsByName("ques" + i);
        for (var x = 0; x < radio.length; x++) {
            if (radio[x].checked) {
                answrs[i-1] = radio[x].value;
            }
        }
    }
    console.log(answrs);
}​

Output: 输出:

When selecting an answer 1, 2, 3, 4, 3, 2, 1, 2, 3, 4 for questions 1-10 the array is: 为问题1-10选择答案1,2,3,4,3,2,1,2,3,4时,阵列为:

["1", "2", "3", "4", "3", "2", "1", "2", "3", "4"]

Hmmm. 嗯。 This worked for me (I've removed some PHP code) 这对我有用(我删除了一些PHP代码)

<html>
<head>
<script type="text/javascript">
    var crct_answrs = new Array();
    var answrs = new Array();

    function check_ans(){

        for(var i = 1; i < 11; i++){
            var tst = "ques"+i;

            var radio = document.getElementsByName(tst);
            alert(radio);
            for (var x = 0; x < radio.length; x ++) {
                if (radio[x].checked) {
                    answrs = radio.value;
                }   
            }
        }       
    }   
</script>
</head>
<body>
<button type="button" onclick="check_ans();">test</button>
            <div id="quest1" class="question">
                <div class="prblm">
                    <?php echo $questions[1]; ?></div>
                <table class="answrs">
                    <tr>
                        <td><?php echo $answrs[1][1]; ?></td>
                        <td><input name="ques1" type="radio" value="1"></td>
                    </tr>
                    <tr>
                        <td><?php echo $answrs[1][2]; ?></td>
                        <td><input name="ques1" type="radio" value="2"></td>
                    </tr>
                    <tr>
                        <td><?php echo $answrs[1][3]; ?></td>
                        <td><input name="ques1" type="radio" value="3"></td>
                    </tr>
                    <tr>
                        <td><?php echo $answrs[1][4]; ?></td>
                        <td><input name="ques1" type="radio" value="4"></td>
                    </tr>
                </table>
            </div>

<body/>
</html>

Any differences from your code? 你的代码有什么不同吗? alert call shows values. 警报呼叫显示值。

I sometimes get an undefined var assigned to document.getElementsByName(name) . 我有时会将未定义的var分配给document.getElementsByName(name)

So I would try to see if any answers from the web would help. 所以我会试着看看网上的任何答案是否会有所帮助。 Alas, nothing worked. 唉,没有用。

So I went ahead and tested the array I thought I should be getting. 所以我继续测试我认为应该得到的阵列。 and the elements were there. 那里的元素。 So just ignore the 'undefined' and proceed working with the var. 所以只需忽略'undefined'并继续使用var。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM