如何使用jquery或javascript获得具有相同名称属性的多个文件的值?

时间:2022-11-27 07:40:10

I am submitting a form using ajax. In the form user can create input fields dynamically and then input data into them. I want that when form is submitted I get all the instances of input fields and then place there values in an array to pass to ajax function. How do I do that? Here is my HMTL code snippet:

我正在使用ajax提交一个表单。在表单中,用户可以动态创建输入字段,然后输入数据。我希望当表单提交时,我得到所有输入字段的实例,然后将数组中的值放置到ajax函数中。我该怎么做呢?以下是我的HMTL代码片段:

<input type="text" name="movies[]" >//user can create as many fields dynamically as they want to and then submit the form.

How do I get the values of all input fields named movies[]?

如何获取所有名为movies[]的输入字段的值?

Here is my jquery code for form:

下面是我的jquery表单代码:

$("#subscription").submit(function(){

//How to get the all the input fields named movies[] values here?

$.ajax({

//Form submission logic using ajax here

});

});

1 个解决方案

#1


3  

You can get the data in an array as follows:

您可以在数组中获取如下数据:

var movies = $('input[name="movies[]"]').map(function(){
   return this.value;
}).get();

now you can pass your movies variable in in ajax function like:

现在您可以在ajax函数中传递movies变量,如:

$.ajax({

//Submit form here and pass your movies variable along other data you want to pass

});

Hope that helps.

希望有帮助。

#1


3  

You can get the data in an array as follows:

您可以在数组中获取如下数据:

var movies = $('input[name="movies[]"]').map(function(){
   return this.value;
}).get();

now you can pass your movies variable in in ajax function like:

现在您可以在ajax函数中传递movies变量,如:

$.ajax({

//Submit form here and pass your movies variable along other data you want to pass

});

Hope that helps.

希望有帮助。