我正在使用ajax提交表单。在表单中,用户可以动态创建输入字段,然后将数据输入其中。我希望在提交表单时获取输入字段的所有实例,然后将值放置在数组中以传递给ajax函数。我怎么做?这是我的HMTL代码段:
<input type="text" name="movies[]" >//user can create as many fields dynamically as they want to and then submit the form.
如何获取所有名为movie []的输入字段的值?
这是我的表单的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函数中传递movie变量,例如:
$.ajax({
//Submit form here and pass your movies variable along other data you want to pass
});
希望能有所帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句