gpt4 book ai didi

javascript - 为什么 Ajax 调用没有执行?

转载 作者:行者123 更新时间:2023-11-30 12:09:33 25 4
gpt4 key购买 nike

我有一个表,其中每条记录都可以被用户删除:

<td style='text-align: center;'>
<button class='btn btn-danger btn-xs delete' id=9 data-title='Delete' data-toggle='modal' data-target='#delete' >
<span class='glyphicon glyphicon-trash'></span>
</button>
</td>

jQuery/AJAX 调用是:

$('.delete').click(function(e) {
var id = $(this).attr('id');
e.preventDefault();
alert(id);
bootbox.confirm("Are you sure? ", function(r) {
if (r) { // Sent request to delete order with given id
alert('SENT REQUEST');
$.ajax({
type: 'GET',
url: 'delete.php',
data: 'id='+id,
success: function(b) {
if (b) { // Delete row
alert('YES');
// orTable.fnDeleteRow($('tr#' + id)[0]);
} else { // Failed to delete
alert('NO');
noty({
text: "There is a problem deleting this record, try again",
layout: "topCenter",
type: "alert",
timeout: 3
});
}
}
});
document.location.reload();
}
});
});

delete.php 是

if($_GET['id']) {
$id = $_GET['id'];
$sql_delete = "DELETE FROM debiteuren WHERE id = " . $id;
mysqli_query($link,$sql_delete) or die(mysqli_error($link));
}

知道为什么它不起作用吗?

我得到警报 ID 和模态“你确定吗?”和警报“发送请求”。但之后页面重新加载,id=9的记录并没有被删除。

亲切的问候,,

阿里

最佳答案

您的 ajax 执行异步尝试在 ajax 完成后重新加载页面:

$('.delete').click(function(e) {
var id = $(this).attr('id');
e.preventDefault();
alert(id);
bootbox.confirm("Are you sure? ", function(r) {
if (r) { // Sent request to delete order with given id
alert('SENT REQUEST');
$.ajax({
type: 'GET',
url: 'delete.php',
data: {id:id},
success: function(b) {
if (b) { // Delete row
alert('YES');
// orTable.fnDeleteRow($('tr#' + id)[0]);
} else { // Failed to delete
alert('NO');
noty({
text: "There is a problem deleting this record, try again",
layout: "topCenter",
type: "alert",
timeout: 3
});

}
document.location.reload();
}
});

}
});

并且正如 Rayon Dabre 建议从 php 页面返回一些内容

注意使用 data-id="9" 为有效的 html

关于javascript - 为什么 Ajax 调用没有执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34202039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com