【问题标题】:how to fire event on file select如何在文件选择上触发事件
【发布时间】:2011-05-09 21:41:17
【问题描述】:

我有一个表格

<form  onSubmit="return disableForm(this);" action="upload.php" method="post" name="f" id="wizecho" enctype="multipart/form-data">
    <input type="file" name="file" />
    <button onClick="return disableForm(this),ajaxUpload(this.form,'wizecho_upload.php', '&lt;br&gt;Uploading image please wait.....&lt;br&gt;'); return false;">
        Upload Image
    </button>
</form>

就是上传图片。

这里我需要点击按钮上传图片,我必须使用onClick 事件。我想删除上传按钮,一旦在输入中选择了文件,我想触发事件。我该怎么做??

【问题讨论】:

标签: javascript jquery


【解决方案1】:

在文件输入上使用更改事件。

$("#file").change(function(){
         //submit the form here
 });

【讨论】:

  • 当您异步提交表单时会发生什么,不要离开页面,然后尝试再次上传相同的文件?此代码只会执行一次,第二次,选择同一个文件不会执行更改事件
  • @ChristopherThomas 的反对正是我在这里的原因,幸运的是,多年后,下面有一个被低估的回应解决了这个问题:stackoverflow.com/a/40581284/4526479
  • 当我再次选择同一个文件时不会触发更改事件。还有什么其他方式每次都能奏效吗?
  • @Tᴀʀᴇǫ Mᴀʜᴍᴏᴏᴅ 请参阅您上面的评论。
  • 答案不使用纯javascript的事实是错误的
【解决方案2】:

您可以在输入字段上订阅 onchange 事件:

<input type="file" id="file" name="file" />

然后:

document.getElementById('file').onchange = function() {
    // fire the upload here
};

【讨论】:

    【解决方案3】:

    这是一个较旧的问题,需要更新的答案来解决@Christopher Thomas 在接受答案的 cmets 中的上述问题。如果您没有离开页面然后再次选择文件,则需要在单击或执行 touchstart(对于移动设备)时清除该值。即使您离开页面并使用 jquery,以下内容也将起作用:

    //the HTML
    <input type="file" id="file" name="file" />
    
    
    
    //the JavaScript
    
    /*resets the value to address navigating away from the page 
    and choosing to upload the same file */ 
    
    $('#file').on('click touchstart' , function(){
        $(this).val('');
    });
    
    
    //Trigger now when you have selected any file 
    $("#file").change(function(e) {
        //do whatever you want here
    });
    

    【讨论】:

    • 这个跨浏览器兼容吗?看起来它只是使用val(''),这在大多数浏览器中都不起作用。
    • 您尝试过哪个浏览器不支持?如果您仍然有问题,请尝试使用它自己克隆对象。
    • 我的问题是使用element.setAttribute("value", "") 如果您不使用jQuery,则需要使用element.value = "" 才能真正正确清除文件元素。
    【解决方案4】:

    在文件加载成功后做任何你想做的事。在文件处理完成后将文件控制的值设置为空白字符串。所以即使文件名更改或不更改,也会始终调用 .change() .例如,您可以做这件事并像魅力一样为我工作

      $('#myFile').change(function () {
        LoadFile("myFile");//function to do processing of file.
        $('#myFile').val('');// set the value to empty of myfile control.
        });
    

    【讨论】:

      【解决方案5】:

      vue 用户解决方案,解决多次上传同一个文件,@change 事件未触发的问题:

       <input
            ref="fileInput"
            type="file"
            @click="onClick"
          />
        methods: {
          onClick() {
             this.$refs.fileInput.value = ''
             // further logic for file...
          }
        }
      

      【讨论】:

      • self.$refs.inputFile.value = ''
      【解决方案6】:

      使用 es6 的原生 js

      document.querySelector('input[name="file"]').addEventListener('change', (e) => {
       const file = e.target.files[0];
        // todo: use file pointer
      });
      

      【讨论】:

        【解决方案7】:
        <input id="fusk" type="file" name="upload" style="display: none;"
            onChange=" document.getElementById('myForm').submit();"
        >
        

        【讨论】:

          【解决方案8】:
          <input type="file" @change="onFileChange" class="input upload-input" ref="inputFile"/>
          
          onFileChange(e) {
              //upload file and then delete it from input 
              self.$refs.inputFile.value = ''
          }
          

          【讨论】:

          • 虽然此代码可能会为问题提供解决方案,但最好添加有关其工作原理/方式的上下文。这可以帮助未来的用户学习并最终将这些知识应用到他们自己的代码中。解释代码时,您也可能会得到用户的积极反馈/赞成。
          猜你喜欢
          • 2020-01-29
          • 2022-01-03
          • 1970-01-01
          • 2019-06-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多