如何使用jQuery获取数组,多个<input>同名 [英] How to get an Array with jQuery, multiple <input> with the same name

查看:51
本文介绍了如何使用jQuery获取数组,多个<input>同名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,用户可以在其中使用 jQuery 添加输入字段.

I have a form where users can add input fields with jQuery.

<input type="text" id="task" name="task[]" />

提交表单后,我在 PHP 中得到一个数组.

After submitting the form I get an array in PHP.

我想用 $.ajax() 处理这个问题,但我不知道如何将我的 s 转换为 jQuery 中的数组.

I want to handle this with the $.ajax() but I have no idea how to turn my <input>s to an array in jQuery.

提前致谢.

推荐答案

使用map:>

var values = $("input[id='task']")
              .map(function(){return $(this).val();}).get();

如果您更改或删除 id(应该是唯一的),您也可以使用选择器 $("input[name='task\\[\\]']")

If you change or remove the id (which should be unique), you may also use the selector $("input[name='task\\[\\]']")

工作示例:http://jsbin.com/ixeze3

这篇关于如何使用jQuery获取数组,多个&lt;input&gt;同名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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