我是 Javascript 的新手 - 第一次实际编码。 我正在尝试使用 SweetAlert 做一个带有删除确认的按钮。当我用
onclick="confirmDelete()"
按下按钮时没有任何反应。这段代码可能只是螃蟹,但它在这里:
<script type="text/javascript">
function confirmDelete() {
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false
)},
$.ajax({
url: "scriptDelete.php",
type: "POST",
data: {id: 5},
dataType: "html",
success: function () {
swal("Done!","It was succesfully deleted!","success");
}
});
}
</script>
<a href="#" onclick="confirmDelete()">Delete</a>
如果删除失败,我可以添加任何警报吗?
如果我正确理解你的问题,你是在问如何处理 ajax 请求中的错误情况。 Ajax settings 有一个 error 属性,可以这样使用
$.ajax({
.... other settings you already have
error: function (xhr, ajaxOptions, thrownError) {
swal("Error deleting!", "Please try again", "error");
}
});
此外,您以错误的方式调用 swal。 Swal 有这样的回调
swal({settings}, function(isConfirm){});
整体代码看起来像这样
function confirmDelete() {
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false
}, function (isConfirm) {
if (!isConfirm) return;
$.ajax({
url: "scriptDelete.php",
type: "POST",
data: {
id: 5
},
dataType: "html",
success: function () {
swal("Done!", "It was succesfully deleted!", "success");
},
error: function (xhr, ajaxOptions, thrownError) {
swal("Error deleting!", "Please try again", "error");
}
});
});
}
试试这个代码。它对我来说工作得很好。
$('.delete-confirm').on('click', function() {
var postID = $(this).val();
console.log(postID);
swal({
title: "Are you sure?",
text: "If you delete this post all associated comments also deleted permanently.",
type: "warning",
showCancelButton: true,
closeOnConfirm: false,
showLoaderOnConfirm: true,
confirmButtonClass: "btn-danger",
confirmButtonText: "Yes, delete it!",
}, function() {
setTimeout(function() {
$.post("delete.php", {
id: postID
},
function(data, status) {
swal({
title: "Deleted!",
text: "Your post has been deleted.",
type: "success"
},
function() {
location.reload();
}
);
}
);
}, 50);
});
});
你做错了
swal({)}
应该是swal({})
更新代码:
<script type="text/javascript">
function confirmDelete() {
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false
},
function(isConfirm){
if (isConfirm) {
$.ajax({
url: "scriptDelete.php",
type: "POST",
data: {id: 5},
dataType: "html",
success: function () {
swal("Done!","It was succesfully deleted!","success");
}
});
}else{
swal("Cancelled", "Your imaginary file is safe :)", "error");
}
})
}
</script>
我终于在 3 年后为那个家伙工作了。
function dropConfig(config_index){
swal({
title: "WARNING:",
text: "Are you sure you want to delete this connection?",
type: "warning",
inputType: "submit",
showCancelButton: true,
closeOnConfirm: true,
timer: 2000
}, //end swal }
function(isConfirm) {
if (isConfirm == true) {
//do the ajax stuff.
$.ajax({
method: "POST",
url: "/drop_config",
data: {"curr_config": $("#curr_conf_conn_name_" + config_index).val()}})
.success(function(msg) {
show_notification(msg,"success");
setInterval(function() {.reload();
}, 2500);})
.error(function(msg) {show_notification(msg.responseText,"danger");});
} // end if }
}); // end function } & end swal )
} // end function }
$("#customerDatatable").on('click', '.Edit', function () {
//var confirmMessageBox = confirm('Are you sure you wish to delete this job ?');
Swal.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
$.ajax({
url: '/api/DairyWorkApi/DeleteClient?Id=' + this.id,
type: "Delete",
data: {'Id': this.id},
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function () {
Swal.fire(
'Deleted!',
'Your file has been deleted.',
'success'
).then(function () {
location.reload();
});
},
error: function (xhr, ajaxOptions, thrownError) {
Swal.fire(
'Cancelled',
'Delete Is Failed',
'error'
);
}
});
}
});
//console.log(this.id);
});`enter code here`