简体   繁体   English

单选按钮返回未定义

[英]Radio button returns undefined

Radio button keeps returning undefined.单选按钮不断返回未定义。 I looked it up, tried answers from other similar questions, but I just get undefined when I add the value to a table.我查了一下,尝试了其他类似问题的答案,但是当我将值添加到表中时,我只是未定义。

This is my javascript code to get the value from the button.这是我从按钮获取值的 javascript 代码。

var grade="";
        var grades = document.getElementsByName('grade');
        for(var i=0;i<grades.length;i++)
        {
            if(grades[i].checked)
                grade = grades[i].value;
        }

And here is my html code这是我的 html 代码

<div><label>Grade</label>
<label><input type="radio" value="5" name="grade">5</label>
<label><input type="radio" value="6" name="grade">6</label>
<label><input type="radio" value="7" name="grade">7</label>
<label><input type="radio" value="8" name="grade">8</label>
<label><input type="radio" value="9" name="grade">9</label>
<label><input type="radio" value="10" name="grade">10</label></div>

I copied this code from a similar page with radio buttons and that one works as intended.我从一个带有单选按钮的类似页面复制了这段代码,该代码按预期工作。 All I did was change the name and values.我所做的只是更改名称和值。

EDIT: This is the code that adds a new row with all the inputted information编辑:这是添加包含所有输入信息的新行的代码

var tbody = document.getElementsByTagName("tbody")[0]
                tbody.innerHTML += "<tr><td>" + name.value + "</td>" + "<td>" + index.value + "</td>" + "<td>" + phone.value + "" +
                    "</td>" + "<td>" + grade.value + "</td>" + "<td>" + session.value + "</td>"
                name.value = ""
                index.value = ""
                phone.value = ""
                grade.value = ""
            }

所以我通过在 tbody.innerHTML 中将 Grade.value 更改为 Grade 来修复它

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

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