我正在使用 ajax 提交表单。在表单中,用户可以动态创建输入字段,然后将数据输入其中。我希望在提交表单时获取输入字段的所有实例,然后将值放在数组中以传递给 ajax 函数。我怎么做? 这是我的 HTML 代码片段:
<input type="text" name="movies[]" >//user can create as many fields dynamically as they want to and then submit the form.
如何获取名为 movies[] 的所有输入字段的值?
这是我的表单 jquery 代码:
$("#subscription").submit(function(){
//How to get the all the input fields named movies[] values here?
$.ajax({
//Form submission logic using ajax here
});
});
最佳答案
可以这样获取数组中的数据:
var movies = $('input[name="movies[]"]').map(function(){
return this.value;
}).get();
现在您可以在 ajax 函数中传递电影变量,例如:
$.ajax({
//Submit form here and pass your movies variable along other data you want to pass
});
希望对您有所帮助。
关于javascript - 如何使用 jquery 或 javascript 获取具有相同名称属性的多个字段的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24206359/