How to clear File Input

前端 未结 11 1058
臣服心动
臣服心动 2020-12-13 05:10

Below is part of the jquery code I have which displays the file input and a \"Clear File\" button.

var $imagefile = $(\'\').attr({
    type: \         


        
相关标签:
11条回答
  • 2020-12-13 06:05

    In my case other solutions did not work than this way:

    $('.bootstrap-filestyle :input').val('');
    

    However, if you will have more than 1 file input on page, it will reset the text on all of them.

    0 讨论(0)
  • 2020-12-13 06:06

    Another solution if you want to be certain that this is cross-browser capable is to remove() the tag and then append() or prepend() or in some other way re-add a new instance of the input tag with the same attributes.

    <form method="POST" enctype="multipart/form-data">
    <label for="fileinput">
     <input type="file" name="fileinput" id="fileinput" />
    </label>
    </form>
    
    $("#fileinput").remove();
    $("<input>")
      .attr({
        type: 'file',
        id: 'fileinput',
        name: 'fileinput'
        })
      .appendTo($("label[for='fileinput']"));
    
    0 讨论(0)
  • 2020-12-13 06:07

    I have done something like this and it's working for me

    $('#fileInput').val(null); 
    
    0 讨论(0)
  • 2020-12-13 06:08

    This is the method I like to use as well but I believe you need to add the bool true parameter to the clone method in order for any events to remain attached with the new object and you need to clear the contents.

        var input = $("#fileInput");
    
        function clearInput() {
            input = input.val('').clone(true);
        };
    

    https://api.jquery.com/clone/

    0 讨论(0)
  • 2020-12-13 06:08

    this code works for all browsers and all inputs.

    $('#your_target_input').attr('value', '');
    
    0 讨论(0)
  • 2020-12-13 06:10

    This should work:

    $imageClear.on('click', function() { 
        $imageFile.val(''); 
    });
    
    0 讨论(0)
提交回复
热议问题