How to clear File Input

前端 未结 11 1059
臣服心动
臣服心动 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:10

    This works to

     $("#yourINPUTfile").val("");
    
    0 讨论(0)
  • 2020-12-13 06:11

    Clear file input with jQuery

    $("#fileInputId").val(null);
    

    Clear file input with JavaScript

    document.getElementById("fileInputId").value = null;
    
    0 讨论(0)
  • 2020-12-13 06:11

    By Setting $("ID").val(null), worked for me

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

    for React users

    e.target.value = ""
    

    But if the file input element is triggered by a different element (with a htmlFor attribute) - that will mean u don't have the event

    So you could use a ref:

    at beginning of func:

    const inputRef = React.useRef();
    

    on input element

    <input type="file" ref={inputRef} />
    

    and then on an onClick function (for example) u may write

    inputRef.current.value = "" 
    
    • in React Classes - same idea, but difference in constructor: this.inputRef = React.createRef()
    0 讨论(0)
  • 2020-12-13 06:13

    get input id and put val is empty like below: Note : Dont put space between val empty double quotes val(" ").

     $('#imageFileId').val("")
    
    0 讨论(0)
提交回复
热议问题