2014-11-22 71 views

回答

4

.delete-person是一個元素被動態地添加到DOM 之後,AJAX調用已經完成。由於您的jQuery腳本在DOMready上運行(當運行時.delete-person不存在)時,click事件將不會綁定到該元素。

該行$('.delete-person').on('click', function() { ... });在功能上與$('.delete-person').click(function() { ... });相同。要點是,您將單擊事件處理程序附加到元素.delete-person,該元素在運行時不在DOM中。

相反,聽click事件從.delete-person起源是冒泡的document對象,而不是:

$(document).on('click', '.delete-person', function() { 
    // Do stuff here to delete person 
}); 

什麼上面的代碼做不同的是,你甚至在document對象監聽點擊,但驗證該點擊事件來自具有.delete-person的類的子元素。由於點擊活動將一直泡到document不管對象是否是在運行時存在或不存在,你就可以刪除這樣的人;)

+0

不'。對()'問題中提及的工作方式和你一樣嗎? – Anubhav 2014-11-22 15:56:22

+0

非常感謝你! – 2014-11-22 15:58:27

+0

不,它不。閱讀文檔:http://api.jquery.com/on/ – Terry 2014-11-22 15:58:33

0

確保有關響應數據 和使用

$(this).closest("tr").remove(); 

或嘗試:

$(".delete_person").on('click',function(){ 
    var element = $(this); 

然後

$(element).closest("tr").remove(); 
0

我想你應該只需添加一個本地JavaScript函數 像這

for (var i = 0 ; i < array.length; i++){ 
       var split = array[i].split("||"); 
       html = html + "<td><a href='#' onlick='deletePerson("+split[1]+")'>Delete</a></td>" // create the new html with the new item 
      } 

然後outsude您的$(document)

function deletePerson(id){ 
    var person_id = id ; 
    //ajax code here 
} 

這是我做什麼,只要我有了使用jQuery

01 remove函數動態表
0

這是因爲元素不存在於文檔中。您需要使用

$(document).on("click", "selector", function(){do something}); 

希望這有助於:)

快樂學習:)