2013-02-10 17 views
0

多個文件上傳。多個文件的輸入,如何獲得預覽的每個文件的預覽,然後找出IE不支持它,我已經改變了我如何去預覽後,如何獲得預覽各自的ID

現在我有多個單個文件輸入,每個都有自己的'預覽'div。每一選擇和預覽div的ID被鏈接(輸入選擇ID =「file1的」,預覽DIV ID =「previews_file1」)等多達10

林使用此代碼來預覽選擇的圖像:

function readURL(input) { 
if (input.files && input.files[0]) { 
    var reader = new FileReader(); 

    reader.onload = function (e) { 
     var thisid = $(this).id; 
     var container = $('#previews_'+ thisid); 
     var image = $('<img>').attr('src', e.target.result).css({'max-width':'200px','max-height':'200px'}); 
     image.appendTo(container); 
    }; 
    reader.readAsDataURL(input.files[0]); 
} 
} 

與HTML:

<div id="previews_file1" style="float:left;width:200px;height:200px;"></div> 
    <input type='file' id="file1" name="file1" onchange="readURL(this);" style="float:left;" /> 

我的問題是我不能似乎在通過輸入ID的功能,當然$(本).ID不起作用。我將如何去預覽到正確的預覽div?

編輯:

爵士小提琴:http://jsfiddle.net/KE3tv/

+0

的jsfiddle控制檯 「未捕獲的ReferenceError:未定義readURL」 – Popnoodles 2013-02-10 13:58:07

回答

1

http://jsfiddle.net/KE3tv/1/

<input type='file' name="file1" /> 
<input type='file' name="file2" /> 
<input type='file' name="file3" /> 

JQ

$('input[type="file"]').on('change', function(){ 
    ... 
    // name is already a unique attribute, no need for an id 
    var container = $('#previews_'+ $(this).attr('name')); 
    ... 
}); 
+0

該W orked,即使我不得不改變的方式,因爲IE再次做這件事,但謝謝你 – rpsep2 2013-02-10 15:20:49