问题描述
但是当我实现甜蜜警报时,它不起作用或返回false。我正在使用sweetalert2
这是我的按钮代码
<a href="http://localhost/app/pages-role-list.PHP?delete_role=18" class="btn btn-sm btn-danger delete-confirm">delete</a>
我的甜蜜初始化
$('.delete-confirm').on('click',function (event) {
event.preventDefault();
const url = $(this).attr('href');
Swal.fire({
title: 'Are you sure?',text: "You won't be able to revert this!",icon: 'question',showCancelButton: true,confirmButtonText: 'Delete'
},function(isConfirm){
if (isConfirm) {
return true;
window.location.href = url;
}else{
return false;
}
})
});
解决方法
回调未使用正确的语法。
这是工作代码:
$('.delete-confirm').on('click',function (event) {
event.preventDefault();
const url = $(this).attr('href');
Swal.fire({
title: 'Are you sure?',text: "You won't be able to revert this!",icon: 'question',showCancelButton: true,confirmButtonText: 'Delete'
}).then((result) => {
if (result.value) {
window.location.href = url;
} else if (result.dismiss === Swal.DismissReason.cancel) {
event.preventDefault();
}
})
});