Jquery delete value from FormData object

风流意气都作罢 提交于 2019-12-23 13:06:59

问题


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.


回答1:


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/




回答2:


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.




回答3:


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


来源:https://stackoverflow.com/questions/41785906/jquery-delete-value-from-formdata-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!