2013-09-21 62 views
7

這是我的問題,我有這個小代碼來顯示img,我沒有重新加載LIVE上傳,但它只適用於一個img,因爲readURL(input)沒有類和直接從noname輸入,當我添加一個類readURL(input.joint),它會降低錯誤!這裏是我的代碼:使用FileReader選擇並顯示圖像

<input class="joint" type='file' id="imgInp" /> 
    <img style="width:45px" id="blah" src="#" alt="your image" /> 


<script type="text/javascript"> 
function readURL(input) { 

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

     reader.onload = function (e) { 
      $('#blah').attr('src', e.target.result); 
     } 

     reader.readAsDataURL(input.files[0]); 
    } 
} 

$("#imgInp").change(function(){ 
    readURL(this); 
}); 
</script> 

我需要添加一些id或類到這個jquery函數,使其獨特的每個輸入。

我想要做的事:

<input class="joint" type='file' id="imgInp" /> 
     <img style="width:45px" id="blah" src="#" alt="your image" /> 


    <script type="text/javascript"> 
    function readURL(input.joint) { 

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

      reader.onload = function (e) { 
       $('#blah').attr('src', e.target.result); 
      } 

      reader.readAsDataURL(input.joint.files[0]); 
     } 
    } 

    $("#imgInp").change(function(){ 
     readURL(this); 
    }); 
    </script> 


<input class="file2" type='file' id="imgInp" /> 
     <img style="width:45px" id="blah" src="#" alt="your image" /> 


    <script type="text/javascript"> 
    function readURL(input.file2) { 

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

      reader.onload = function (e) { 
       $('#blah').attr('src', e.target.result); 
      } 

      reader.readAsDataURL(input.file2.files[0]); 
     } 
    } 

    $("#imgInp").change(function(){ 
     readURL(this); 
    }); 
    </script> 
+0

這不是非常清楚你想要做什麼。你得到的錯誤是什麼? – Jivings

+0

錯誤我的意思是什麼都沒有工作:(對不起我的英文,也許我不能正確解釋我的問題!輸入功能需要與id或類 – user2784722

+0

你期待從這裏:'readURL(input.joint)'? – Jivings

回答

10

您有重複的ID各地(imgInpblah),所以選擇將只選擇第一個,最後你的事件僅結合第一個輸入字段以及$('#blah')的選擇。您需要選擇相對於輸入的相對圖像。您也不必重複該腳本。選擇下圖像標記相對於輸入像$(input).next('img').attr('src', e.target.result);

的那麼嘗試:

function readURL(input) { 

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

     reader.onload = function (e) { 
      $(input).next('img').attr('src', e.target.result); 
     } 

     reader.readAsDataURL(input.files[0]); 
    } 
} 


$(function(){ 
    $(".upld").change(function() { //set up a common class 
     readURL(this); 
    }); 
}); 

Fiddle

+0

也是一個很好的解決方案!謝謝 – user2784722

+0

是的,對我來說這個效果更好,因爲我試圖buld類似facebook的上傳系統沒有任何閃光燈,這是我的網站,臨時照片導入作品像s ** t的一個大問題,所以需要更好解! jquery是答案,像魔術一樣工作;)現在我需要像X這樣的東西刪除,接下來的chalange!但隨着您的解決方案,它將成爲easyer做刪除和其他的東西! – user2784722

+0

@ user2784722永遠不會在您的代碼中重複ID。事實上,使用ID的特別是在處理jQuery時可以使用類名來定位元素。也可以嘗試將腳本從標記中分離出來,並將選擇放入document.ready中。這是因爲你的腳本在DOM中的元素之後。 – PSL

1

您的代碼正常工作對我來說,當我解決這個錯字:

if (input.filers && input.files[0]) { 
      ^^^^^^ 
if (input.files && input.files[0]) { 

這裏是例子:http://jsfiddle.net/s6ttw/

更新

如果喲你希望它能夠工作,你期望你需要使用類而不是ID。

這裏是你問什麼是更好的例子:http://jsfiddle.net/s6ttw/1/

+0

我的代碼工作,我知道,但如果我想多個圖像顯示在一個頁面上?這是我的案例 – user2784722

+0

好的,我編輯了我的帖子 – Jivings

+0

WOW!太棒了,你能解釋一下你是怎麼做到的嗎? – user2784722

2

好,您正在嘗試的事實類添加到與點函數參數符號說明您需要了解有關Javascript如何工作的詳細信息,可以在簡單的StackOverflow答案中解釋,但讓我試試:

  • input in readURL(input)是一個參數名稱,類似於變量名稱,所以不能以任何方式擴展它。當您撥打readURL時,這是對您傳遞的任何內容的參考,在這種情況下,如果您查看代碼的結尾,則是對this的引用。 this引用您傳入的jQuery選擇器引用的元素:"#imgInp"

所以,如果你想使用.joint類代替,只是通過在一個jQuery選擇:

$(".joint").change(function(){ 
    readURL(this); 
}); 
  • 但是,假設你有多個.joint元素,你readURL功能將無更長時間的正常工作,因爲它目前僅用於更改#blahsrc。你需要做的是改變每個元素imgsrc

所以,如果你有一些HTML這樣的:

<input class="joint" type='file' /> 
<img class="joint-img" style="width:45px" src="#" alt="your image" /> 

<input class="joint" type='file' /> 
<img class="joint-img" style="width:45px" src="#" alt="your image" /> 

<input class="joint" type='file' /> 
<img class="joint-img" style="width:45px" src="#" alt="your image" /> 

您可以將所有的元素後,請將您的readURL功能和改變reader.onload部分如下:

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

     reader.onload = function (e) { 
      $(input).next('.joint-img').attr('src', e.target.result); 
     } 

     reader.readAsDataURL(input.files[0]); 
    } 
} 

$(".joint").change(function(){ 
    readURL(this); 
}); 
+0

更詳細的答案,不錯的一個。 – Jivings

+0

感謝您的回答,這也會對我有幫助! – user2784722