javascript Javascript获取表单数组值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19010177/
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 form array values
提问by user577732
I have a form that is set up so that there is an add button so my user can submit multiple people at once to the site.
我有一个表单,它设置了一个添加按钮,以便我的用户可以一次向站点提交多人。
At first the form has an input to fill out the persons name one example below
起初,表单有一个输入来填写人名,下面是一个例子
<input type="text" name="name[]" value="Name" />
If they hit the add button another one is appended so that I'm left with the following
如果他们点击添加按钮,则会附加另一个按钮,以便我留下以下内容
<input type="text" name="name[]" value="Name" />
<input type="text" name="name[]" value="Name" />
Then when they hit submit I'm trying to get the values of each name in Javascript and loop through them I've tried this but it's not working
然后当他们点击提交时,我试图在 Javascript 中获取每个名称的值并循环遍历它们我试过这个,但它不起作用
var inputs = document.getElementById('formIDhere').getElementsByTagName('name');
alert(inputs.length);
I never get the alert and if I just set up a loop like this
我从来没有收到警报,如果我只是设置了这样的循环
for(int i=0; i<inputs.length; i++)
Nothing happens, any help on looping through the values of name[] in javascript would be greatly appreciated
没有任何反应,任何有关在 javascript 中循环访问 name[] 值的帮助将不胜感激
采纳答案by Duncan
I guess you could try:
我想你可以试试:
var inputs = document.querySelectorAll("#formIDHere input[name='name[]']");
alert(inputs.length);
for (i = 0; i < inputs.length; i++) {
// your code here
}
回答by Biswadeep Sarkar
Simple approach:
简单的方法:
var names=document.getElementsByName('name[]');
for(key=0; key < names.length; key++) {
alert(names[key].value);
//your code goes here
}
回答by Анатол?й ЖелезнЫйквадрат
[...document.querySelector("#FORMID").elements['name[]']].map(el=>el.value);
// Returns ["name","name",...]
回答by Shadow
getElementsByTagName
returns the array of elements whoose tag name is specified in argument.
In html there is no element whoose tag name is name
.
getElementsByTagName
返回在参数中指定了标签名称的元素数组。在 html 中,没有标签名称为 的元素name
。
name
is an attribute given to html elements.
To get elements based on name you can use
name
是赋予 html 元素的属性。要根据名称获取元素,您可以使用
var ele=document.getElementsByName('whatevername');
ele
will contain the array of elements whose name is specified. Then you can use your loop to iterate through each element.
ele
将包含指定名称的元素数组。然后您可以使用循环遍历每个元素。
回答by Tony Brix
this is what form elements is for
这就是表单元素的用途
var inputs = document.getElementById('formIDhere').elements["name[]"];
alert(inputs.length);
for (i = 0; i < inputs.length; i++) {
// your code here
}
回答by Halcyon
I think you can use document.getElementById('formIDhere').elements.name
.
我认为你可以使用document.getElementById('formIDhere').elements.name
.
It will give you an array with all the values.
它会给你一个包含所有值的数组。
回答by Labib Ismaiel
your mistake is using getElementsByTagName
, which is asking for a tag called <name>
, and of course you don't have it, try setting a class to the input for example and fetch it using jquery $('className')
which will surely get the result correct, or using dom you can still use
document.getElementById('form').elements.name
but still this way might not be cross browser safe unless tested carefully
你的错误是使用getElementsByTagName
,它要求一个名为 的标签<name>
,当然你没有它,例如尝试为输入设置一个类并使用 jquery 获取它$('className')
,这肯定会得到正确的结果,或者使用 dom 你可以仍在使用,
document.getElementById('form').elements.name
但除非仔细测试,否则这种方式可能不是跨浏览器安全的