简体   繁体   English

关闭一个后立即打开引导程序模式会导致第一个无法正确关闭

[英]Opening bootstrap modal right after closing one results in first one not closing properly

I'm using modals from bootstrap 5.1, but I'm encountering some issues.我正在使用来自 bootstrap 5.1 的模式,但我遇到了一些问题。

The intended behaviour I want is:我想要的预期行为是:

  • User presses a button, and a "Confirm modal" appears.用户按下一个按钮,然后出现“确认模式”。
  • User presses "Confirm" on the modal, it gets closed and a function with an ajax get call is executed.用户在模式上按下“确认”,它被关闭并执行 function 和 ajax get 调用。
  • Once the ajax get returns, the "Result modal" is opened with the result of the operation一旦 ajax get 返回,“Result modal”打开并显示操作结果

But this happens only sometimes, since most of the times, being the ajax call really fast, it's like the first modal is not able to fully close itself, leaving the background dark (the background gets even darker once the second modal opens, and when I close it, it returns to the first shade of dark).但这只是有时发生,因为大多数时候,ajax 调用非常快,就像第一个模态无法完全关闭自身,使背景变暗(一旦第二个模态打开,背景变得更暗,并且当我关闭它,它返回到第一个黑暗阴影)。

This is how my two modals are defined in the html:这就是我的两个模态在 html 中的定义方式:

<div class="modal fade" id="modal_confirm" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="modal_confirm_title" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="modal_confirm_title">Confirm Operation</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                Some body
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                <button id="confirm_btn" type="submit" class="btn btn-primary" data-bs-dismiss="modal" onclick="someFunction()">Confirm</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="modal_result" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="modal_result_title" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="modal_result_title">Operation result</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                Operation result
            </div>
            <div class="modal-footer">
                <button type="submit" class="btn btn-primary" data-bs-dismiss="modal">OK</button>
            </div>
        </div>
    </div>
</div>

This is the javascript:这是 javascript:

function operation()
{
    let modal = new bootstrap.Modal(confirmation_modal, {"keyboard": true});
    $("#confirm_btn").on("click", performOperation)
    modal.show();
}


function performOperation()
{
    let xhr = $.ajax({
        url: "SOME_URL",
        method: "GET",

        success: function (response)
        {

            if (response['errorCode'] !== undefined)
            {
                let modal = new bootstrap.Modal(result_modal, {"keyboard": true});
                $("#security_modal_result_title").text("Operation failed")
                modal.show();
            }
            else
            {
                let modal = new bootstrap.Modal(result_modal, {"keyboard": true});
                $("#security_modal_result_title").text("Operation succeeded")
                modal.show();
            }
        },

        error: function ()
        {
            showError();
        }
    });
}

What am I missing here?我在这里错过了什么?

Thanks in advance.提前致谢。

you never call function operation() , your confirm_btn dont have the event onclick because is never assigned.您永远不会调用function operation() ,您的confirm_btn没有事件 onclick 因为从未分配过。

<button id="confirm_btn" type="submit" class="btn btn-primary" data-bs-dismiss="modal" onclick="performOperation()">Confirm</button>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM