[英]Sweet Alert confirm dialog disappears before response?
我对Sweet Aler确认对话框有疑问。 当我单击删除图标时,Sweet Alert对话框显示的时间少于一秒钟,然后消失并删除该元素。 换句话说,我没有机会单击“删除”或“取消”。 如何停止此对话框让我选择一个选项?
<a href="insert.php?delete=20"><i class="fa fa-trash-o fa-2x pull-right trashbin"></i></a>
---
<script type="text/javascript">
document.querySelector('i.trashbin').onclick = function(event){
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!',
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm){
if (isConfirm){
swal("Deleted!", "Your imaginary file has been deleted.", "success");
} else {
swal("Cancelled", "Your imaginary file is safe :)", "error");
}
});
};
</script>
尝试添加event.preventDefault();
后:
document.querySelector('i.trashbin').onclick = function(event) {
您必须将'a'标记中的'src'值替换为'#',并将'delete = 20'传递给函数。 然后在该函数中,如果用户单击“是”,则重定向到使用传递给它的参数指定的页面。 这解决了我的问题。 希望对您也有帮助。
** 编辑 **
<a href="#"><i class="fa fa-trash-o fa-2x pull-right trashbin"></i></a>
---
<script type="text/javascript">
document.querySelector('i.trashbin').onclick = function () {
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!',
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: false
}).then(function () {
swal({timer: 1000, title: "Deleted!", type: "success"});
setTimeout(function goAndDel() {
window.location.assign("insert.php?delete=20");
}, 1000);
});
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.