[英]Javascript Radio Button Values returns undefined
I am having lots of difficulty with getting the value from a radio button group in Javascript.我在从 Javascript 中的单选按钮组中获取值时遇到了很多困难。 I have searched for hours and have not found the solution.我已经搜索了几个小时并没有找到解决方案。 When I try to do a document.write command to show what the value is from the radio button selection, they all come back as "undefined".当我尝试执行 document.write 命令以显示单选按钮选择的值时,它们都以“未定义”的形式返回。 But the funny thing is, if I select the first radio button, it does come back with the correct value shown.但有趣的是,如果我选择第一个单选按钮,它会返回显示的正确值。 This is part of my frustration and I haven't found the solution to find out how to get the other values in the radio button group, this should be SIMPLE!这是我沮丧的一部分,我还没有找到解决方案来找出如何在单选按钮组中获取其他值,这应该很简单! Your help will be greatly appreciated.对你的帮助表示感谢。
Please take a close look at my Javascript code to try to get the value (please disregard the return false; command as I use it to stop the rest of the code on my page):请仔细查看我的 Javascript 代码以尝试获取该值(请忽略 return false; 命令,因为我使用它来停止我页面上的其余代码):
for( i = 0; i < document.forms.incident.elements.model.length; i++ )
{
if(document.forms.incident.elements.model[i].checked)
var omodcheck = document.forms.incident.elements.model.value;
document.write(omodcheck);
return false;
}
My HTML form name is incident.我的 HTML 表单名称是事件。 The form code for the radio button group is as follows:单选按钮组的表单代码如下:
Laptop Model:
<input type="radio" name="model" value="Lenovo X200" />Lenovo X200<br />
<input type="radio" name="model" value="Lenovo X201" />Lenovo X201<br />
<input type="radio" name="model" value="Lenovo X200T" />Lenovo X200T<br />
<input type="radio" name="model" value="Lenovo X201T" />Lenovo X201T<br />
<input type="radio" name="model" value="Lenovo T400" />Lenovo T400<br />
<input type="radio" name="model" value="Lenovo R400" />Lenovo R400<br />
In this case, the code for the document.write(omodcheck) returns the value "Lenovo X200", however, if I make a selection for any of the other radio buttons, the value comes back "undefined"!在这种情况下,document.write(omodcheck) 的代码返回值“Lenovo X200”,但是,如果我选择任何其他单选按钮,则返回值“未定义”!
Please help!请帮忙!
If I understand it correctly, you will need something like this:如果我理解正确,您将需要这样的东西:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<script type="text/javascript">
function checkChecked(){
for (var i = 0; i< document.getElementsByName('model').length; i++) {
if(document.testform["model"][i].checked){
var omodcheck = document.testform["model"][i].value
alert(omodcheck);
}
}
}
</script>
</head>
<body onload="checkChecked()">
Laptop Model:
<form name="testform">
<input type="radio" name="model" value="Lenovo X200" onchange="checkChecked()" />Lenovo X200<br />
<input type="radio" name="model" value="Lenovo X201" onchange="checkChecked()" />Lenovo X201<br />
<input type="radio" name="model" value="Lenovo X200T" onchange="checkChecked()" />Lenovo X200T<br />
<input type="radio" name="model" value="Lenovo X201T" onchange="checkChecked()" />Lenovo X201T<br />
<input type="radio" name="model" value="Lenovo T400" onchange="checkChecked()" />Lenovo T400<br />
<input type="radio" name="model" value="Lenovo R400" onchange="checkChecked()" checked />Lenovo R400<br />
</form>
</body>
</html>
or或者
use brackets on your if condition:在你的 if 条件上使用括号:
for( i = 0; i < document.forms.incident.elements.model.length; i++ )
{
if(document.forms.incident.elements.model[i].checked){
var omodcheck = document.forms.incident.elements.model[i].value;
document.write(omodcheck);
}
}
From what I understand, it's a very small mistake in that the for-loop is ending on your return false statement.据我了解,这是一个非常小的错误,因为 for 循环以您的 return false 语句结束。 The return false must be outside of the for loop. return false 必须在 for 循环之外。
Take a look at this jsfiddle (slightly modified to use alert() instead of document.write();看看这个jsfiddle (稍微修改为使用 alert() 而不是 document.write();
btnSubmit.onclick = function(){
for( i = 0; i < document.forms.incident.elements.model.length; i++ )
{
if(document.forms.incident.elements.model[i].checked)
var omodcheck = document.forms.incident.elements.model.value;
}
alert(omodcheck);
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.