jQuery从FormData对象中删除值 [英] Jquery delete value from FormData object

查看:278
本文介绍了jQuery从FormData对象中删除值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从具有相同名称的FormData对象中删除值? 我有带两个输入文件的HTML表单.

how I can delete value from FormData object with same name? I have HTML form with two input files.

<input id="human" type="file" name="file[]" class="docfiles" />
<input id="animal" type="file" name="file[]" class="docfiles" />

例如,我要删除文件1-ID为"human".知道怎么做吗?

For example I want to delete file 1 - with id "human". Any idea how to do this?

这是我的演示 jsfiddle .

推荐答案

处理文件数组并重新添加减去需要删除的元素的元素.

Manipulate the array of files and re-add the elements minus the one needed to be removed.

var files = formData.getAll("file[]");
files.splice($("[type='file']").index($("#animal")), 1);
formData.delete("file[]");
$.each(files, function(i, v) {
    formData.append("file[]", v);
});

演示 https://jsfiddle.net/nnte528L/

这篇关于jQuery从FormData对象中删除值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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