【问题标题】:Sweet Alert confirm dialog disappears before response?Sweet Alert 确认对话框在响应之前消失?
【发布时间】:2015-09-10 10:40:36
【问题描述】:

我遇到了 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>

【问题讨论】:

    标签: javascript sweetalert


    【解决方案1】:

    尝试在后面添加event.preventDefault();

    document.querySelector('i.trashbin').onclick = function(event) {
    

    【讨论】:

    • 现在它使用 event.preventDefault() 停止操作;但确认后它不会删除元素...:/
    【解决方案2】:

    您必须将 '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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多