Javascript IE 输入文件属性未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5000950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:10:55  来源:igfitidea点击:

IE input file attribute is undefined

javascriptjqueryinternet-explorermozilla

提问by ShaneKm

I have the following input file tag:

我有以下输入文件标签:

<input type="file" id="handlerxhr1" />

In mozilla when I run the following jQuery code:

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

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

        });

I get response: [object File] (which is good).

我得到响应:[object File](很好)。

But in IE I get 'input.files.0 is undefined'

但在 IE 中我得到“input.files.0 未定义”

What am I doing wrong?

我究竟做错了什么?

采纳答案by bcm

This seems good enough...

这似乎已经足够了...

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

Not sure if your were after something like this though:

不确定你是否在追求这样的事情:

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

回答by Lalit

IE doesn't support .files[0] property, whereas FF does. See http://www.w3.org/TR/FileAPI/for more details

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