Javascript 如何使用jQuery获取数组,多个具有相同名称的<input>
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2627813/
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
How to get an Array with jQuery, multiple <input> with the same name
提问by x4tje
I have a form where users can add input fields with jQuery.
我有一个表单,用户可以在其中使用 jQuery 添加输入字段。
<input type="text" id="task" name="task[]" />
After submitting the form I get an array in PHP.
提交表单后,我在 PHP 中得到一个数组。
I want to handle this with the $.ajax()but I have no idea how to turn my <input>s to an array in jQuery.
我想用 来处理这个问题,$.ajax()但我不知道如何将我的<input>s 转换为 jQuery 中的数组。
Thanks in advance.
提前致谢。
回答by Kobi
Using map:
使用地图:
var values = $("input[id='task']")
.map(function(){return $(this).val();}).get();
If you change or remove the id (which should be unique), you may also use the selector $("input[name='task\\[\\]']")
如果您更改或删除 id(应该是唯一的),您也可以使用选择器 $("input[name='task\\[\\]']")
Working example: http://jsbin.com/ixeze3
工作示例:http: //jsbin.com/ixeze3
回答by Sarfraz
For multiple elements, you should give it a class rather than id eg:
对于多个元素,你应该给它一个类而不是 id 例如:
<input type="text" class="task" name="task[]" />
Now you can get those using jquery something like this:
现在你可以得到那些使用 jquery 的东西:
$('.task').each(function(){
alert($(this).val());
});
回答by Dal Hundal
Firstly, you shouldn't have multiple elements with the same ID on a page - ID should be unique.
首先,页面上不应有多个具有相同 ID 的元素 - ID 应该是唯一的。
You could just remove the id attribute and and replace it with:
您可以删除 id 属性并将其替换为:
<input type='text' name='task'>
and to get an array of the values of task do
并获取任务值的数组 do
var taskArray = new Array();
$("input[name=task]").each(function() {
taskArray.push($(this).val());
});
回答by fxs
To catch the names array, i use that:
要捕获名称数组,我使用它:
$("input[name*='task']")
回答by rahul
You can't use same id for multiple elements in a document. Keep the ids different and name same for the elements.
您不能对文档中的多个元素使用相同的 id。保持元素的 id 不同和名称相同。
<input type="text" id="task1" name="task" />
<input type="text" id="task2" name="task" />
<input type="text" id="task3" name="task" />
<input type="text" id="task4" name="task" />
<input type="text" id="task5" name="task" />
var newArray = new Array();
$("input:text[name=task]").each(function(){
newArray.push($(this));
});
回答by Antonio Reyes
HTML:
HTML:
<input type="text" name="task[]" class="form-control" id="task">
JS:
JS:
var tasks= new Array();
$('input[name^="task"]').each(function()
{
tasks.push($(this).val());
});
回答by suraj kakade
Q:How to access name array text field
问:如何访问名称数组文本字段
<input type="text" id="task" name="task[]" />
Answer - Using Input name array :
答案 - 使用输入名称数组:
$('input[name="task\[\]"]').eq(0).val()
$('input[name="task\[\]"]').eq(index).val()
回答by wade
if you want selector get the same id, use:
如果你想让 selector 得到相同的 id,使用:
$("[id=task]:eq(0)").val();
$("[id=task]:eq(1)").val();
etc...
回答by Nikola Obreshkov
You can use jquery.serializeJSONto do this.
您可以使用jquery.serializeJSON来执行此操作。

