【发布时间】:2012-06-24 23:24:10
【问题描述】:
我正在做某事并且问题得到了解答,但我需要它的不同用途。 我有一个关于它如何工作的 jsFiddle。 http://jsfiddle.net/TbZzH/4/
这很好,很花哨,但是当我在我的代码中这样做时,它会告诉我 data.files[0] 不起作用,并且据说是未定义的。它也无法识别 FileReader() 对象。
我的代码如下,以jsFiddle为例。
$(function(){
$("input[type='file'].attribute").on("change", function () { updateDesigner(this); });
});
function updateDesigner(input){
var t = input;
if ($(input).attr("type") == 'file'){
try{
var data = $(t)[0];
var file = data.files[0]; //<------ FAILS HERE. .files is an undefined attr.
var reader = new FileReader(); //<--- working around it, doesnt understand this object as well
reader.onload = function (e) {
value = e.target.result;
}
reader.readAsDataURL(file);
}catch(errrrr){
alert("error putting image into image tag: "+errrrr.toString());
}
}
srcFunction(value); //takes the value and applied it to the src attr of the image tag.
}
我想将数据转化为价值,一切都会顺利进行,但我不确定发生了什么。
【问题讨论】:
-
只需使用
input.files[0]。我不知道 $(t)[0] 应该做什么? -
是的,我试过了,但也没有用。它说文件未定义等。“无法获取属性 0 的值,它为 null 或未定义”,因为文件似乎未定义。
-
哦,一个大问题:我在 IE 中运行。我正在查看 Object.keys($(input)[0]) 和.... IE 无法识别它们中的蚂蚁,并且只返回一个 jqueryobjectreference 字符串。在 GoogleChrome 中运行它具有所有对象并绕过此错误。啊。浏览器特定的 bs 太烦人了
-
不要在IE中开发!曾经! :-)
-
我希望这是一件事。我必须,公司政策有一个跨板的网络应用程序,并且可以追溯到大约 IE8
标签: javascript jquery image file input