Javascript 如何按名称获取元素并设置其值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10339426/
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
How to get an element by name and set its value
提问by Stephen305
There should be a simple solution for this. I need to get an input element by name and set its value.
应该有一个简单的解决方案。我需要按名称获取输入元素并设置其值。
The following Javascript does not work:
以下 Javascript 不起作用:
x = document.getElementsByName($('#questions').val());
x.value=this.value;
Is there a simple solution using JQuery?
是否有使用 JQuery 的简单解决方案?
回答by dknaack
Description
描述
You are mixing normal javascript and jQuery. Use the attribute selector.
您正在混合普通的 javascript 和 jQuery。使用属性选择器。
Check out my sample and this jsFiddle Demonstration
查看我的示例和这个jsFiddle 演示
Sample
样本
Html
html
<input type="text" name="nameOfTheInputElement"/>
jQuery
jQuery
$(function() {
$("input[name='nameOfTheInputElement']").val("your value");
});
?
Edit
编辑
If you want, for some reason, change a element which name is a value in another element then do this. jsFiddle Demonstration
如果您出于某种原因想要更改名称是另一个元素中的值的元素,请执行此操作。jsFiddle 演示
Html
html
<input type="text" id="questions" value="nameOfTheInputElement"/>
<input type="text" name="nameOfTheInputElement"/>
?jQuery
? jQuery
$(function() {
var name = $("#questions").val();
$("input[name='"+name +"']").val("your value");
});?
More Information
更多信息
回答by ajax333221
getElementsByName()returns a node-list, so you need to get the first one getElementsByName(...)[0]
getElementsByName()返回一个节点列表,因此您需要获取第一个getElementsByName(...)[0]
But you are already using jQuery, so use it. Read some tutorials about the jQuery selectors
但是您已经在使用 jQuery,因此请使用它。阅读一些关于jQuery 选择器的教程
回答by Elias Van Ootegem
A simple, pure JavaScript (and therefore faster) solution:
一个简单、纯 JavaScript(因此更快)的解决方案:
var x = document.getElementsByName(document.getElementById('questions').value)[0].value = this.value;
I know jQuery's tagline is 'Write less, do more', and in many cases it is true... many cases !== always
, though ;-)
我知道 jQuery 的标语是“少写,多做”,而且在很多情况下这是真的... many cases !== always
,不过 ;-)
回答by Flavio Cysne
Try this
尝试这个
var q = $("#question").val();
var x = $("input[name='" + q + "']").val();
on the 2nd line, variable q, the name provided in input with id 'question', will be enclosed with ' and could contain any supported characters, like space, :, -, etc
在第 2 行,变量 q,即输入中提供的 id 为“问题”的名称,将用 ' 括起来,并且可以包含任何受支持的字符,例如空格、:、- 等
If you need the value of a component regardless of its tag, you can do this:
如果你需要一个组件的值而不考虑它的标签,你可以这样做:
var x = $("[name='" + q + "']").val();
Consider that this approach $("[name='" + q + "']")
can return more than one element, but .val()
will return only the value of the first element.
考虑到这种方法$("[name='" + q + "']")
可以返回多个元素,但.val()
只会返回第一个元素的值。
回答by Jamie Dixon
If I'm understanding your question correctly, you want to get an element by name, and the name of the element you're selecting will be specified by some dropdown or text area.
如果我正确理解您的问题,您希望按名称获取元素,并且您选择的元素的名称将由某些下拉列表或文本区域指定。
Here's my take on it:
这是我的看法:
Html
html
<input type="text" id="enteredName">
<button id="doTheStuff">Do the work</button>
<input name="one" value="One">
<input name="two" value="Two">
jQuery
jQuery
$('#doTheStuff').click(function(){
var objectOfInterest = $('[name=\'' + $('#enteredName').val() + '\']');
alert(objectOfInterest.val());
});
Here's another working exampleusing a dropdown to specify the selection name.