How to clear File Input

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

    
    

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

提交回复
热议问题