admin 管理员组文章数量: 1086019
Ok, so I have form for creating polls. I want to use AJAX request and able user to attach image instead of question, so I use FormData for this.
I could't find any solution for working with multiple input with same name (named like this: "name[]"). I tried this option:
var fdata = new FormData();
fdata.append('answers[]', $('input[name="answer[]"]').val());
But it doesn't work. I know I could use .each()
, but I don't want different name for each question, so I don't have to rebuild PHP side too much.
Thanks for any help.
Ok, so I have form for creating polls. I want to use AJAX request and able user to attach image instead of question, so I use FormData for this.
I could't find any solution for working with multiple input with same name (named like this: "name[]"). I tried this option:
var fdata = new FormData();
fdata.append('answers[]', $('input[name="answer[]"]').val());
But it doesn't work. I know I could use .each()
, but I don't want different name for each question, so I don't have to rebuild PHP side too much.
Thanks for any help.
Share Improve this question edited Jul 20, 2015 at 14:45 isherwood 61.2k16 gold badges121 silver badges170 bronze badges asked Jul 20, 2015 at 13:32 r34r34 3323 silver badges15 bronze badges 02 Answers
Reset to default 4You have to append each value in turn. Currently you are only appending the first one (because that is what val()
returns.
$('input[name="answer[]"]').each(function (index, member) {
var value = $(member).val();
fdata.append('answers[]', value);
});
The problem is $('input[name="answer[]"]').val()
isn't giving you what you need; it returns the first input element's value. Instead, you want an array of values:
var values = [];
$('input[name="answer[]"]').each(function(i, item) {
values.push(item.value);
});
fdata.append('answers[]', values);
http://jsfiddle/j5ezgxe9/
本文标签: javascriptSubmitting multiple inputs with same nameStack Overflow
版权声明:本文标题:javascript - Submitting multiple inputs with same name - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1744097770a2533215.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论