IE输入文件属性未定义

Sha*_*eKm 5 javascript jquery internet-explorer mozilla

我有以下输入文件标记:

<input type="file" id="handlerxhr1" />
Run Code Online (Sandbox Code Playgroud)

在mozilla中,当我运行以下jQuery代码时:

var input = $('#handlerxhr1')[0];
        $('#upload').click(function() {
            alert(input.files[0]);

        });
Run Code Online (Sandbox Code Playgroud)

我得到回复:[对象文件](这很好).

但是在IE中我得到'input.files.0 is undefined'

我究竟做错了什么?谢谢.

Lal*_*lit 6

IE不支持.files [0]属性,而FF则支持.files [0]属性.有关详细信息,请参见http://www.w3.org/TR/FileAPI/

  • 那么,什么是跨浏览器选项? (26认同)
  • 是否有任何外部库用于处理IE9中的文件? (2认同)

bcm*_*bcm 4

这看起来已经足够好了...

$(function() {
    var input = $('#handlerxhr1')[0];         
    $('#upload').click(function() {             
        alert(input);          
    }); 
});
Run Code Online (Sandbox Code Playgroud)

不确定你是否在追求这样的东西:

$(function() {
    var input = $('#handlerxhr1')[0];         
    $('#upload').click(function() {             
        var x = $('input[type=file]:eq(0)');
        alert(x);
    }); 
});
Run Code Online (Sandbox Code Playgroud)

  • `$('#handlerxhr1')[0]` 与 `$('#handlerxhr1')` 相同,因为 jquery 返回一个匹配元素的数组。它的作用与“files[0]”不同。 (2认同)