[英]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.