Javascript获取表单数组值 [英] Javascript get form array values

查看:129
本文介绍了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
}

这篇关于Javascript获取表单数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆