我正在使用window.FileReader提供用户选择上传的图像的即时预览。未设置默认图像
我有以下问题:
A)中的代码行63-77似乎被忽略,虽然当我打印的“i”值在第25行,我可以看到这是在增加(我怀疑它必须与window.Filereader的性质有关,但不确定)。
if (i == 0) //if this is the first picture add it as primary too
{
var primaryimage = ' <img id=' + filename + ' height="220" width="220" src=' + this.result + ' /> ';
$('#primary-pic').find('.custom-input-file').hide();
$('#primary-pic').find('p').hide();
$('#primary-pic').append(primaryimage);
$('.custom-input-file').show();
}
B)如果添加了多张图片,点击“设置为默认”由于某种原因,这似乎并没有为最后的图像,但不知道为什么工作。
感谢
开始的时候,你的代码是嵌套的,看起来你错过了一些变量作用域,比如你的'onloadend'处理函数中的'i',这个函数被调用时的最终值'i' 。另外,您多次添加$(“。setDefault:button”)处理程序。如果我是你,我会使用事件委托,以便处理程序只能添加一次到周围的元素。 – MasterAM