0

我对 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>
4

2 回答 2

1

尝试添加event.preventDefault();后:

document.querySelector('i.trashbin').onclick = function(event) {
于 2015-09-16T21:15:31.267 回答
1

您必须将 '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>
于 2017-09-19T10:17:00.827 回答