Javascript 使用ajax加载时,从sql中删除而不刷新页面不起作用
我有聊天室,我正在通过Javascript 使用ajax加载时,从sql中删除而不刷新页面不起作用,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有聊天室,我正在通过ajax $.ajax({ type: "GET", url: "chat/mini.php", success: function(chat){ $('.chat-message').html(chat); }, error: function(err) { console.log(err); } }); 我在chat/mini.php中有要删除的链接 <a href="#" id="
ajax
$.ajax({
type: "GET",
url: "chat/mini.php",
success: function(chat){
$('.chat-message').html(chat);
},
error: function(err) {
console.log(err);
}
});
我在chat/mini.php中有要删除的链接
<a href="#" id="'.$row['id'].'" class="delbutton text-danger" title="Delete">X</a>
当我不使用ajax
加载聊天时,这些cod工作,但当我使用ajax
加载聊天时,这些cod不工作。我必须添加一些东西才能工作?更改$(“.delbutton”)。单击(函数(){
到$(“.delbutton”)。在('click',函数(){
上,您检查了我说的内容的原因吗?是的,我检查了,但没有工作,因为html(由chat/mini.php返回的那个)是动态的,当处理动态创建的html元素的事件时,您应该使用jquery委托。您可以使用旧方法来执行此操作,并调用[delegate方法]()或$(“.chat message”)。在('click'、'.delbutton',function(){})上;立即更改其工作状态$(“.delbutton”)。单击(function(){/code>到$(.delbutton”)。打开('click',function(){
为什么您检查了我说的内容?是的,我检查了,但不工作,因为html(由chat/mini.php返回的html)是动态的,当处理动态创建的html元素的事件时,您应该使用jquery委派。您可以使用旧方法,并调用[delegate方法]()或$(“.chat message”)。on('click','.delbutton',function(){});它现在可以工作了
$(".delbutton").click(function () {
//Save the link in a variable called element
var element = $(this);
//Find the id of the link that was clicked
var del_id = element.attr("id");
//Built a url to send
var info = 'id=' + del_id;
if (confirm("Sure you want to delete? There is NO undo!")) {
$.ajax({
type: "GET",
url: "chat/delete.php",
data: info,
success: function () {
}
});
}
return false;
});