【问题标题】:how to use onchange element correctly for IE如何为 IE 正确使用 onchange 元素
【发布时间】:2019-09-29 04:39:05
【问题描述】:

对于上传文件,我使用这种形式:

<div id="drop_file_zone" ondrop="upload_file(event)" ondragover="return false">
  <div id="drag_upload_file">                                               
    <p>DROP FILE(S) HERE</p>
    <p>or</p>

    <p><input class="browse btn" type="button" id="browse" value="Browse" onclick="file_explorer();"></p>
    <input type="file" id="selectfile" name="upload" multiple>

   </div>
</div>

js的这一部分

 function file_explorer() {

        document.getElementById('selectfile').click();
        document.getElementById('selectfile').onchange = function() {
            for (var i=0; i< this.files.length;i++){ // multiple files uploading
              fileobj = document.getElementById('selectfile').files[i];
              ajax_file_upload(fileobj);
            }
        };

    }

上传可在所有浏览器中使用,IE 除外。我已经发现 IE 和 onchange 存在问题。

有没有办法可以重写函数file_explorer 使其在 IE 中也能正常工作?

【问题讨论】:

    标签: javascript jquery internet-explorer upload onchange


    【解决方案1】:

    尝试使用 JavaScript 方法附​​加 ondrop 功能,请修改您的代码如下:

    <head>
        <meta charset="utf-8" />
        <title></title>
        <script>
            function myFunction() {
                document.getElementById('selectfile').ondrop = function () {                    
                    document.getElementById('selectfile').click();
                    document.getElementById('selectfile').onchange = function () {
                        for (var i = 0; i < this.files.length; i++) { // multiple files uploading
                            fileobj = document.getElementById('selectfile').files[i];
                            //ajax_file_upload(fileobj);
                        }
                    };
                };
            };
        </script>
    </head>
    <body onload="myFunction()">
        <div id="drop_file_zone" ondragover="return false">
            <div id="drag_upload_file">
                <p>DROP FILE(S) HERE</p>
                <p>or</p>
    
                <p><input class="browse btn" type="button" id="browse" value="Browse" onclick="file_explorer();"></p>
                <input type="file" id="selectfile" name="upload" multiple>
    
            </div>
        </div>
    </body>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-09
      • 2011-04-25
      • 1970-01-01
      相关资源
      最近更新 更多