最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - IE input file attribute is undefined - Stack Overflow

programmeradmin3浏览0评论

I have the following input file tag:

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

In mozilla when I run the following jQuery code:

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

        });

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

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

What am I doing wrong?

I have the following input file tag:

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

In mozilla when I run the following jQuery code:

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

        });

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

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

What am I doing wrong?

Share Improve this question edited Dec 7, 2019 at 13:11 Cœur 38.7k26 gold badges203 silver badges277 bronze badges asked Feb 15, 2011 at 7:35 ShaneKmShaneKm 21.3k46 gold badges175 silver badges307 bronze badges 3
  • 1 try alert(typeof(input.files)); in IE – Umair A. Commented Feb 15, 2011 at 7:37
  • It might be affected by the way IE handles JS differently than firefox. The click event you have putten on your upload button fires after the upload is done on firefox, and before its done on IE. – Jan Dragsbaek Commented Feb 15, 2011 at 7:38
  • can't be. because click event i declared var input when document.ready and after it's loaded i click '#upload' button – ShaneKm Commented Feb 15, 2011 at 7:40
Add a ment  | 

2 Answers 2

Reset to default 6

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

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);
    }); 
});
发布评论

评论列表(0)

  1. 暂无评论