简体   繁体   English

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

[英]How do I get the values of multiple fileds having same name attribute using jquery or javascript?

I am submitting a form using ajax. 我正在使用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. 我希望在提交表单时获取输入字段的所有实例,然后将值放置在数组中以传递给ajax函数。 How do I do that? 我怎么做? Here is my HMTL code snippet: 这是我的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[]? 如何获取所有名为movie []的输入字段的值?

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

});

});

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函数中传递movie变量,例如:

$.ajax({

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

});

Hope that helps. 希望能有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 jquery 和 javascript 获取类属性值 - How do I get the class attribute value using jquery and javascript 如何使用具有相同名称的jquery获取每个输入字段的值? - How to get values of each input field using jquery having same name? javascript / jquery ajax:具有相同名称属性的输入会发生什么 - javascript/jquery ajax: what happens with inputs having same name attribute 如何使用jQuery获取div中隐藏元素的名称属性? - How do I get the name attribute of a hidden element within a div using jQuery? 如何获取与javascript数组同名的input元素的值? - How do I get values of input element with the same name as an javascript array? 如何从 javascript 中相同的多个 div 类名中获取特定值? - How do I get the particular value from same multiple div class name in javascript? 如何获取Javascript中HTML属性的某些值? - How do I get certain values of a HTML attribute in Javascript? 使用jQuery和JavaScript,如何区分具有相同名称属性的两个XML级别? - Using jQuery and JavaScript, how do I distinguish between two XML levels that have the same Name attr? 如何在 JavaScript class 中有多个同名方法? - How do I have multiple methods with the same name in a JavaScript class? 当有多个具有相同名称的单选按钮时,如何检查选择了哪个单选按钮? (使用jquery) - How do I check which radio button is selected when there are multiple radio buttons with the same name? (using jquery)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM