salal警报后,jquery bootstrap模态关闭 [英] jquery bootstrap modal closing after swal alert

查看:82
本文介绍了salal警报后,jquery bootstrap模态关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里有此代码

单击确定swal后,我试图关闭bootstrap模态:

I am trying to close bootstrap modal after swal is clicked ok:

swal({
    title:'', 
    text:'Thanks, we will contact you !', 
    type: 'success'
});

$("#modal").on('hidden.bs.modal', function () {
    alert('boo');
    $(this).data('bs.modal', null);
}).trigger();     

什么都没有发生,甚至是alert都没有,只有当我单击模式上的关闭按钮时,它会显示alert并关闭.

Nothing is happening, not even alert, only if I click the close button on the modal, it shows an alert and closes.

推荐答案

我发现的答案没有用,因为hide.bs.modal甚至在返回SWAL的承诺之前就被触发了.这是我的工作职能:

The answers I found didn't work, because the hide.bs.modal even is triggered before the promise from SWAL is returned. This is my working function:

        $('#myModal').on("hide.bs.modal", function (e) {

        if (!$('#myModal').hasClass('programmatic')) {
            e.preventDefault();
            swal.fire({
                title: 'Are you sure?',
                text: "Please confirm that you want to cancel",
                type: 'warning',
                showCancelButton: true,
                confirmButtonText: 'Yes, cancel',
                cancelButtonText: 'No, I clicked by mistake',
            }).then(function(result) {
                if (result.value) {
                    $('#myModal').addClass('programmatic');
                    $('#myModal').modal('hide');
                    e.stopPropagation();

                } else {
                    e.stopPropagation();

                }
            });

        }
        return true;
    });

    $('#myModal').on('hidden.bs.modal', function () {
        $('#myModal').removeClass('programmatic');
    });

如您所见,每当代码触发close事件时,我都会向模式中添加一个类,因此我使用e.preventDefault()来防止模式被关闭.每当成功隐藏模式后,我都会再次删除该类,因此它将在用户下次尝试关闭它时再次起作用.

As you can see, I add a class to the modal whenever the close event is triggered by the code, so I use e.preventDefault() to prevent the modal from closing. Whenever the modal has been successfully hidden, I remove that class again, so it will work again the next time the user tries to close it.

这篇关于salal警报后,jquery bootstrap模态关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆