Bootbox dialog.modal('hide') 不隐藏模态

Bootbox dialog.modal('hide') doesn't hide modal

我对 bootbox.js 模态框有疑问。我想在执行 Ajax 并等待响应时使用 bootbox.dialog 暂停 UI。如果我在 bootbox.alert 中使用 dialog.modal('hide'); 并确认(单击“确定”后它会隐藏 dialog.modal),一切都会很好,但我不想每次都确认它。当我在 bootbox.alert 之外使用 dialog.modal('hide'); 并确认它不会隐藏 dialog.modal... 问题出在哪里?工作代码(隐藏模态)在 success 内,不工作在 error

var dialog = bootbox.dialog({
            message: '<p class="text-center mb-0"><i class="fa fa-spin fa-cog"></i> Please wait while we do something...</p>',
            closeButton: false
        });
        var checkboxId = "#" + $(this).attr('id');
        var checked = $(this).is(":checked");
        if (checked) {
            $.ajax({
                url: url,
                type: "POST",
                data: { estimatedCostId: @Model.EstimatedCostID },
                success: function (data) {
                    if (!data.Success) 
                    {
                        bootbox.alert(data.ErrorMessage, function () {
                            dialog.modal('hide');
                        });
                        $(checkboxId).prop('checked', !checked);
                    }
                },
                error: function (request, status, error) {
                    dialog.modal('hide');
                    bootbox.alert("Błąd serwera");
                    $(checkboxId).prop('checked', !checked);
                }
            });
        }

这很可能是时间问题。如果您的 AJAX 调用是 "too quick",则 success/fail 回调在 bootbox.dialog 函数解析之前被调用。所以,这个:

dialog.modal('hide');

最终在未定义的对象上被调用。我在最近的项目中 运行 遇到了类似的问题,并通过将 AJAX 调用放入 shown.bs.modal 事件中解决了这个问题,如下所示:

var checkboxId = "#" + $(this).attr('id');
var checked = $(this).is(":checked");

var dialog = bootbox.dialog({
    message: '<p class="text-center mb-0"><i class="fa fa-spin fa-cog"></i> Please wait while we do something...</p>',
    closeButton: false
})
 // this is the change
.on('shown.bs.modal', function(){
    if (checked) {
        $.ajax({
            url: url,
            type: "POST",
            data: { estimatedCostId: @Model.EstimatedCostID },
            success: function (data) {
                if (!data.Success) 
                {
                    bootbox.alert(data.ErrorMessage, function () {
                        dialog.modal('hide');
                    });
                    $(checkboxId).prop('checked', !checked);
                }
            },
            error: function (request, status, error) {
                dialog.modal('hide');
                bootbox.alert("Błąd serwera");
                $(checkboxId).prop('checked', !checked);
            }
        });
    }
});

你也可以用这个简单的方法

bootbox.hideAll();