2011-07-15 33 views
0

我正在使用表單向創建圖像的頁面提交詳細信息,表單使用Jquery提交,以便用戶不必離開頁面。 send()函數調用refresh()函數來更新放置圖像的div ...jquery get image

我遇到了一個代碼行問題,我不知道放什麼來得到它的工作:

function refresh() 
{ 
    $(function() { 
     var img = new Image(); 
     $(img).load(function() { 
      //$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already 
      $(this).hide(); 
      $('#loader').removeClass('loading').append(this); 
      $(this).fadeIn(); 
     }).error(function() { 
      // notify the user that the image could not be loaded 
// The next line causes the error 
     }).attr('src', "signatures/" +$("#username") "png"); 
    }); 

} 

任何幫助,將不勝感激。

+0

有什麼錯誤? – scottm

回答

0

看起來像你需要的+ $("#username").val() + ".png")代替+$("#username") "png")

這是假設用戶名是某種隱藏輸入的ID領域。相應地用其他東西替換val()

+0

我知道我錯過了一些東西,你是一個生命的救星,感謝大家的有益貢獻:) – Andrew

1

你進一步擴展之前horgot點:

.attr('src', "signatures/" +$("#username") "png"); 

應該是:

.attr('src', "signatures/" +$("#username") ".png"); 

如果$("#username")是一個值。 你可能需要一個html的()/的.text或.VAL(),...

+0

對不起,這是我自己的錯誤有一個。在那裏。仍然無法加載。 – Andrew

+0

看到編輯,$('用戶名')是一個對象(或數組) – beardhatcode

0

試試這個

function refresh() 
{ 
    $(function() { 
     var img = $("<img />").appendTo(document.body); 
     $(img).load(function() { 
      //$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already 
      $(this).hide(); 
      $('#loader').removeClass('loading').append(this); 
      $(this).fadeIn(); 
     }).error(function() { 
      // notify the user that the image could not be loaded 
// The next line causes the error 
     }).attr('src', "signatures/" +$("#username")+ ".png"); 
    }); 

} 
+0

不,似乎沒有工作:( – Andrew

+0

檢查我編輯答案。 – ShankarSangoli

0
 
}).attr('src', "signatures/" +$("#username") "png"); 

你傳遞一個DOM對象,$( 「#用戶名」)。如果「用戶名」元素是一個文本框,你可以使用:

 
$("#username").val() + ".png" 

而如果是一個div或一些其他類型的容器,你可以使用:

 
$("#username").text() + ".png"