javascript Javascript通过单选按钮名称获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6895317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript get value by radio button name
提问by Side
first of all i am a beginner in javascript, a really big beginer
首先,我是 javascript 的初学者,一个非常大的初学者
Can someone give me a hint on this?
有人可以给我一个提示吗?
I have a form
, on the form i have a radio
button.
我有一个form
, 在表单上我有一个radio
按钮。
And i would like to that if the radio is set yes , it would show the value of it on another page.
我希望如果收音机设置为 yes ,它会在另一个页面上显示它的价值。
And i would like to get the value by the input name
我想通过输入名称获取值
is it possible?
是否可以?
I'm not asking to write my code, but just an example for a start.
我不是要写我的代码,而只是一个开始的例子。
i was tried with this just for a test
我试过这个只是为了测试
<input type="hidden" name="the_name" value="yes">
if(the_name.element == 'yes') {
alert('cool');
}
回答by AlienWebguy
the_name.value
, not the_name.element
the_name.value
, 不是 the_name.element
回答by RobG
You can reference the form using:
您可以使用以下方式引用表单:
document.forms[formName or formId].elements[element-name];
However, name is not necessarily unique in the document. If there is only one element with the name, then only one is returned. If more than one have the same name, then you will get a collection (a bit like an array) and you will have to iterate over them to find the one you want.
但是,名称在文档中不一定是唯一的。如果只有一个具有名称的元素,则只返回一个。如果有多个同名,那么您将得到一个集合(有点像数组),您必须遍历它们以找到您想要的集合。
You can also use document.getElementsByName, which always returns a collection. So you can do:
您还可以使用document.getElementsByName,它始终返回一个集合。所以你可以这样做:
var elements = document.getElementsByName('the_name');
// get the one with value 'yes'
for (var i=0, iLen=elements.length; i<iLen; i++) {
if (elements[i].value == 'yes') {
return elements[i];
}
}
You will also need to be careful of case sensitivity, you might want to use toLowerCase()on the value first, or use a case insensitive regular expression:
您还需要注意区分大小写,您可能希望首先对值使用toLowerCase(),或者使用不区分大小写的正则表达式:
e.g.
例如
var re = /yes/i;
...
if (re.test(elements[i].value)) {
// value is Yes or YES or yeS or ...
回答by Sojourner
You can use getElementsByName()
for accessing the elements value via the input name. But as a standard and since it helps load off the DOM we use getElementById()
instead of the name. Also you can start from here -> http://eloquentjavascript.net/
您可以getElementsByName()
用于通过输入名称访问元素值。但作为一个标准,因为它有助于加载 DOM,我们使用getElementById()
而不是名称。你也可以从这里开始 -> http://eloquentjavascript.net/
回答by Nivas
<input type="hidden" name="the_name" id="the_name" value="yes"> <!-- id added -->
if(formName.the_name.value == 'yes')
{
alert('cool');
}
OR better (since you are not accessing the elements by global names)
或者更好(因为您不是通过全局名称访问元素)
var element = document.getElementById("the_name");
if(element.value === 'yes')
{
alert("yes, it is");
}