Javascript获取表单数组值
我设置了一个表单,以便有一个添加按钮,以便我的用户可以一次将多个人提交到该站点.
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" />
然后,当他们点击提交时,我试图获取Javascript中每个名称的值并遍历它们,我已经尝试过了,但是没有用
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
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++)
什么也不会发生,对通过javascript中的name []值循环进行的任何帮助将不胜感激
Nothing happens, any help on looping through the values of name[] in javascript would be greatly appreciated
我想您可以尝试:
var inputs = document.querySelectorAll("#formIDHere input[name='name[]']");
alert(inputs.length);
for (i = 0; i < inputs.length; i++) {
// your code here
}