Jquery delete value from FormData object

后端 未结 3 465
说谎
说谎 2021-01-18 04:05

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



        
相关标签:
3条回答
  • 2021-01-18 04:52

    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.

    0 讨论(0)
  • 2021-01-18 05:10

    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 讨论(0)
  • 2021-01-18 05:12

    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/

    0 讨论(0)
提交回复
热议问题