Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

清空 input[type="file"] 的值 #29

Open
chbro opened this issue Apr 23, 2018 · 4 comments
Open

清空 input[type="file"] 的值 #29

chbro opened this issue Apr 23, 2018 · 4 comments

Comments

@chbro
Copy link

chbro commented Apr 23, 2018

let resetFileDom = dom => {
    if (({}).toString.call(dom) !== '[object HTMLInputElement]') {
        throw new Error('必须传入DOM节点')
        return
    }
    dom.value = ''
    dom.type = 'text'
    dom.type = 'file'
}
@xmcp
Copy link

xmcp commented Apr 23, 2018

直接 dom.value='' 不行吗?

@Youcongcong
Copy link

@chbro 近期我也有操作上传文件一直想清空😊。试了你的方案成功清空。👍

@Yangfan2016
Copy link

@xmcp 可以直接设置为空('' | null) (chrome 69 firefox 62 edge17)
https://codepen.io/Yangfan2016coding/pen/aaXoez?editors=1111

@isbasex
Copy link

isbasex commented Nov 1, 2018

直接 dom.value='' 有兼容性问题

考虑兼容性,也可以这样

<form id="uploadField">
  <input type="file" />
</form>
window.uploadField.reset()

但是要注意 form 不能嵌套 form,最好 Portal 到其他地方

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants