簡體   English   中英

IE8和IE9上的Javascript錯誤

[英]Javascript error on IE8 and IE9

我得到'this.0.files.0'為null或IE8和IE9上沒有對象錯誤,Chrome和Mozila不會拋出任何錯誤。

$(function()) {
    var fileType = ['txt' , 'csv' ];
    $('.input_file').find('input [type = "file" ]').live('change', function (e)) {
        $this = $(this) 
        var ext = $this.val() === " " ? " " : this.value.match(/\.(.+)$/)[1];
        if($this.val()) {
            $this.parent().find('label').text.($this[0].files[0].name)  
        }
    }
}

我不知道為什么上面的代碼拋出一個javascript錯誤'this.0.files.0'是null或不是一個對象

IE <10不支持html5 fileapi,即沒有HTMLInputElement.FileList您必須解析HTMLInputElement.value才能獲取文件名。

要獲取文件名,您可以:

var filename = $this[0].files ? $this[0].files[0].name : $this[0].value.match(/[^\\/\\\\]*$/)[0];

或者干脆:

$this[0].value.match(/[^\\/\\\\]*$/)[0];

完整代碼:

$(function()) {
    var fileType = ['txt' , 'csv' ];
    $('.input_file').find('input [type = "file" ]').live('change', function (e)) {
        $this = $(this) 
        var ext = $this.val() === " " ? " " : this.value.match(/\.(.+)$/)[1];
        if ($this.val()) {
            var filename = $this[0].files ? $this[0].files[0].name : $this[0].value.match(/[^\/\\]*$/)[0];
            $this.parent().find('label').text.($this[0].files[0].name);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM