2014-11-22 75 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}); 

希望这有助于:)

快乐学习:)