Javascript 从数据库中删除记录而不刷新
我有一个代码,它应该删除数据而不刷新。删除过程正常,但我必须刷新才能删除数据。 这是我的密码请帮帮我 阿贾克斯:Javascript 从数据库中删除记录而不刷新,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我有一个代码,它应该删除数据而不刷新。删除过程正常,但我必须刷新才能删除数据。 这是我的密码请帮帮我 阿贾克斯: $(function () { $(".trash").click(function () { var del_id = $(this).attr("id"); var info = 'id=' + del_id; if (confirm("Sure you want to delete this post? This cannot be undone l
$(function () {
$(".trash").click(function () {
var del_id = $(this).attr("id");
var info = 'id=' + del_id;
if (confirm("Sure you want to delete this post? This cannot be undone later.")) {
$.ajax({
type: "POST",
url: "delete.php", //URL to the delete php script
data: info,
success: function () {}
});
$(this).parents(".record").animate("fast").animate({
opacity: "hide"
}, "slow");
}
return false;
});
});
这是我的html:
<td style="padding-left: 23px">
<img class="photo" data-toggle="modal" data-target="#gallery<?php echo $photo; ?>" src="<?php echo $r1['photo']; ?>" />
<div class="hotel">
<button class="trash" id="<?php echo $r1['photo_id']; ?>" > <span class="glyphicon glyphicon-remove" aria-hidden="true"></span></button>
</div>
</td>
" />
您可以向父tr提供数据映像id attr
<tr data-image-id="<?php echo $r1['photo_id']; ?>">
您的代码正常工作,可能您需要显示完整的html,或者至少显示类“.record”来分析“error”无需为表行(TR)中的ID添加额外属性
试试这个:
函数删除_值(id)
{
如果(确认('是否确实要删除此?'))
{
//ajax从数据库中删除数据
$.ajax({
url:“/”+id,
类型:“POST”,
成功:功能(数据)
{
$(“tr[id=“+id+”])。删除();
},
错误:函数(jqXHR、textStatus、errorshown)
{
警报(“删除数据时出错”);
}
});
}
}
你的浏览器控制台中有什么?我是ajax新手,所以我不知道如何绑定。显示的代码引用了类。记录,但显示的HTML中没有该类元素。当你说“不刷新”时“,您的意思是您确实希望从页面中删除该项目,但不重新加载整个页面吗?是的。我使用的i代码删除了记录,但在刷新页面后,我仍然可以在页面中看到它,它将消失。@AnecitoAlimaSantillan-
$("tr[data-image-id="+del_id+"]").remove();
$('.glyphicon-remove').on('click',function() {
$(this).closest( 'tr').remove();
return false;
});