2012-11-26 38 views
0

我有以下的HTML代码jQuery的获取IE造成错误9

<label class="fileinput-button" id="fileUpload-label"> 
    <span class="btn btn-add" id="fileUpload-span">Add a file..</span> 
     <div id="fileinput-outer"> 
     <input type="file" name="files[]" id="fileUpload"> 
     </div> 
</label> 

然后我使用jQuery获得与输入类型的文件列表=文件

$('#fileUpload').live('change', function() { 
    $.map($('#fileUpload').get(0).files, function (file) { 
     $('#fileList') 
      .append(CreateFileList(file.name, true)); 
     }); 
}); 

这伟大工程Chrome和Firefox,但在IE中的错误是

Unable to get value of the property 'length': object is null or undefined 

所以我开始麻烦,并有此代码

var arrFiles[]; 
arrFiles = $('#fileUpload').get(0).files; 
    if(arrFiles.length === 0) { 
     alert('empty');  
    } else { 
     alert('has value');  
    } 

Chrome和Firefox - 代码,并提醒如预期,但仍IE抛出错误

Unable to get value of the property 'length': object is null or undefined 

好像IE可能与jQuery的问题找..

任何人知道如何我可以解决这个错误在IE浏览器有或没有JQuery的?

+0

有你尝试使用'.data('files',files)'而不是'.get(0).files = files'来存储文件?在IE9之前,我有问题将数据绑定到DOM。 –

回答

5

jQuery().get工作正常,你还没打破它。然而,没有files财产上<input type=file />为IE 9,它在IE 10中引入见这个例子http://jsfiddle.net/RtqnZ/

你还缺少对multiple财产您input

参考:http://msdn.microsoft.com/en-us/library/ie/hh772931(v=vs.85).aspx

+0

当我在IE中运行jsFiddle时,出现错误http://jsfiddle.net/RtqnZ/ < - Line:35错误:无法获取属性'length'的值:object为null或undefined –

+0

@ DavidKEgghead?当然,这是我的观点,它不适用于IE <10' –