[英]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.