在单击“删除”按钮后,甜警报似乎运行良好,但是ajax成功函数中的警报作为确认没有出现在页面上。
我在编码方面真的很新,我在youtube上学习了一个关于从php数据库中删除数据的教程。
我认为我做的一切都是正确的,并重新检查是否有拼写或标点符号错误或什么,但我不知道它是什么。求你帮帮我。
$(document).ready(function() {
$('.delete_product_btn').click(function(e){
e.preventDefault();
var id = $(this).val();
swal({
title: "Are you sure?",
text: "Once deleted, you will not be able to recover this data!",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
$.ajax({
method: "POST",
url: "code.php",
data: {
'product_id':id,
'delete_product_btn': true
},
success: function (response) {
if(response == 200)
{
swal("Good job!", "Deleted successfully!", "success");
}
else if(response == 500)
{
swal("Error!", "Something went wrong!", "error");
}
}
});
}
});
});
});发布于 2022-10-07 13:17:04
首先,假设如果记录未被删除,code.php将返回"500“值;如果成功删除记录,则返回"200”值。(所以在删除操作之后,可以是echo "200";还是echo "500";)
其次,您应该包含适当的jquery.js脚本(比如放在脚本的顶部)。
在这种情况下,这两个swal警报都应该可以工作,但是如果code.php不返回500或200,那么当然不会触发第二个swal警报。(这可能是问题的根本原因,但由于我看不到您的PHP代码,我无法进一步评论,所以请确保code.php将返回200或500)
因此,请尝试以下代码:(我已经创建了一个始终是code.php的echo "200";)
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
<script
src="https://code.jquery.com/jquery-3.6.1.js"
integrity="sha256-3zlB5s2uwoUzrXK3BT7AX3FyvojsraNFxCc2vC/7pNI="
crossorigin="anonymous"></script>
Delete:
<br>
<input type=button class="delete_product_btn" value="1">
<br>
<input type=button class="delete_product_btn" value="2">
<script>
$(document).ready(function() {
$('.delete_product_btn').click(function(e){
e.preventDefault();
var id = $(this).val();
swal({
title: "Are you sure?",
text: "Once deleted, you will not be able to recover this imaginary file!",
icon: "warning",
buttons: true,
dangerMode: true,
})
.then((willDelete) => {
if (willDelete) {
$.ajax({
method: "POST",
url: "code.php",
data: {
'product_id':id,
'delete_product_btn': true
},
success: function (response) {
if(response == 200)
{
swal("Good job!", "Deleted successfully!", "success");
}
else if(response == 500)
{
swal("Error!", "Something went wrong!", "error");
}
}
});
} else {
swal("Delete action aborted!");
}
});
});
});
</script>您可以在这个完全工作的沙箱链接中尝试上面的脚本:
http://www.createchhk.com/SOanswers/testso7Oct2022.html
https://stackoverflow.com/questions/73986417
复制相似问题