文件上传时对input标签的重置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_37064409/article/details/78971703
  • 在使用input上传文件时,通常会绑定onchange事件,但是文件上传后input的value值不会主动清空,以至于再次上传同名文件时不会触发onchange事件,所以需要手动清空。三种方法如下:

  • html结构

    <form action="" id="uploads">
        <input type="file" id="fileUp"/>
    </form>
  • js代码:
    1# 将整个form表单重置
        $("#uploads")[0].reset();  //重置

    2# 将当前input标签的value值设为空
        document.getElementById("fileUp").value = "";

    3# 重新加载该DOM节点 <能实现,但不推荐使用>
    var upDom = document.getElementById("fileUp")
    upDom.outerHTML = upDom.outerHTML;

猜你喜欢

转载自blog.csdn.net/weixin_37064409/article/details/78971703