最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery delete value from FormData object - Stack Overflow

programmeradmin5浏览0评论

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" />

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

Here my demo jsfiddle.

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" />

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

Here my demo jsfiddle.

Share Improve this question edited Jan 22, 2017 at 0:16 didsun asked Jan 22, 2017 at 0:00 didsundidsun 1111 gold badge3 silver badges11 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 15

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);
});

Demo https://jsfiddle.net/nnte528L/

You can delete the entire file list by setting the value property of the input object to an empty string.

document.getElementById('human').value = "";

If you are using FormData, you can also have

ForEach(var key in formData.keys()){
  formData.delete(key);
}

This will iterate through all keys in formData and delete all the key value pairs.

if you're using jQuery you can use

$("#human").remove();

if you're using vanilla JS you can do the following

var human = document.getElementById('human');
human.parentNode.removeChild(human);
发布评论

评论列表(0)

  1. 暂无评论