我得到'this.0.files.0'为空或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或者不是对象
答案 0 :(得分:11)
IE< 10不支持html5 fileapi,即没有HTMLInputElement.FileList
你必须解析HTMLInputElement.value
才能获得文件名。
答案 1 :(得分:1)
要获取您可以执行的文件名:
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);
}
}
}