2012-06-19 61 views
2

我在我的網站上有一個儀表板,其中包含一些表中的條目,表中的每個條目都有一個delete按鈕。當用戶點擊delete按鈕時,會發生Ajax調用,並且正在刪除回調函數中的該條目。我的代碼:刪除ajax回調問題

$(".del").live({ 
    click: function() { 
     $.post("/Home/DeleteTemplate", { 
      name: $(this).parent().siblings("td:first").children("a").html() 
     }, function (data) { 
      $(this).parents("tr").remove(); //Inside the callback 
     }); 
    } 
}); 

現在我的問題是,如果我在回調函數中刪除該行並沒有從項目立即刪除該行。我必須關閉並再次打開儀表板才能看到結果。

$(".del").live({ 
    click: function() { 
     $.post("/Home/DeleteTemplate", { 
      name: $(this).parent().siblings("td:first").children("a").html() 
     }); 
     $(this).parents("tr").remove(); //Outside the callback 
    } 
}); 

這裏有什麼問題:

但如果我刪除回調函數的外部進入然後在同一時間刪除?

回答

4

this並不是指你的選擇裏面的回調,這樣做:

$(".del").live({ click: function() { 
    var that = $(this); // added this 

    $.post("/Home/DeleteTemplate", 
     { name: that.parent().siblings("td:first").children("a").html() }, 
     function (data) { 
     that.parents("tr").remove(); // used "that" here 
     }); 
    } 
}); 
+0

奈斯利發現那裏。 + 1 – Anand

+1

@Anand:謝謝兄弟:) – Sarfraz

+0

在數據中將'$(this)'改爲'that'' – 2012-06-19 11:28:33