如何组合两个javascript FormData对象 [英] How to combine two javascript FormData objects

查看:176
本文介绍了如何组合两个javascript FormData对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要组合两个FormData对象并使用XMLHttpRequest发布它们。其中一个表单包含文件输入。

I need to combine two FormData objects and post them using XMLHttpRequest. One of the forms contains file input.

var formData = new FormData(document.forms.namedItem('form-ship'));
var poData = new FormData(document.forms.namedItem('po-form'));

// Combine them
var fData = $.extend(true, formData, poData);

当我使用 $时,它不起作用.extend 或者如果我使用 serialize()来组合没有文件输入的表单。知道怎么做吗?

It doesn't work when I use $.extend or if I use serialize() to combine the form that doesn't have file input. Any idea how to do this?

推荐答案

你做不到。 FormData 遗憾的是,不是可枚举的。

You cannot. FormData is unfortunately not enumerable.

但是,正如您所说,只有一个表单确实包含文件输入。然后应该可以使用 serializeArray 另一个和 追加 手动数据:

However, as you say only one of your forms does contain a file input. Then it should be possible to use serializeArray on the other and append to the data manually:

var formData = new FormData(document.forms['form-ship']); // with the file input
var poData = jQuery(document.forms['po-form']).serializeArray();
for (var i=0; i<poData.length; i++)
    formData.append(poData[i].name, poData[i].value);

这篇关于如何组合两个javascript FormData对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆