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

查看:28
本文介绍了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天全站免登陆