2011-12-15 153 views
2

我嘗試上傳系統並使用Uploadify。我有onComplete功能,其中apend圖像和刪除按鈕。每張圖片都有自己的刪除按鈕,但我無法刪除它的圖片。請幫助這裏是我的代碼。謝謝。jQuery逐個刪除元素

enter code here 
'onComplete' : function(event, ID, fileObj, response, data) { 

    $('#inner2').append('<img id="img" src="'+fileObj.filePath+'" width="100" height="100" /><div id="remove-btn" ><span>Remove Image</span></div>'); 

    $("#remove-btn").live('click',function() { 

     $(this).parent().remove(); 

    }); 

} 
+0

創建元素時,不應使用「live」。使用`bind`或者在onComplete處理程序之外分配`live`處理程序。 – Dennis 2011-12-15 13:21:10

回答

1

嘗試選擇previous圖片。也就是說,以往的點擊DIV:

$(this).prev("img").andSelf().remove(); 
+0

好吧,這是工作,但按鈕不會消失 – 2011-12-15 13:18:33

+0

@Ivan伊萬諾夫 - 編輯。很確定它現在就做你想做的事。 – karim79 2011-12-15 13:30:45

0
$(this).parent().find('your image tag here').remove(); 
0
$(this).prev().remove(); 
$(this).remove(); 

正如我猜你也想刪除的按鈕,因爲它是原位缺口存在了的圖像。

或者你可以將圖像和按鈕包裝在一個div中然後你的.parent()很好。