简体   繁体   English

引导模式隐藏不适用于 jquery ajax

[英]Bootstrap modal hide not working with jquery ajax

I have a modal dialog made with bootstrap, like this:我有一个用引导程序制作的模态对话框,如下所示:

<div class="modal fade" id="myid">
  <div class="modal-dialog">
    <div class="modal-content">
       Loading...
    </div>
  </div>
</div>

And I make a javascript function like this:我做了一个 javascript function 像这样:

$(document)
            .ajaxStart(function () {
                $('#myid').modal('show');
            })
            .ajaxStop(function () {
                $('#myid').modal('hide');
            });

But when I call some ajax function the modal appears but no hide.但是当我调用一些 ajax function 时,模态出现但没有隐藏。 I put console.log in both functions (ajaxStart and ajaxStop) and it was called.我将 console.log 放在两个函数(ajaxStart 和 ajaxStop)中,它被调用了。 when I replace ajaxStop as below, everything works, but not appears right.当我如下替换 ajaxStop 时,一切正常,但看起来不正确。

$(document)
                .ajaxStart(function () {
                    $('#myid').modal('show');
                })
                .ajaxStop(function () {
                    $('#myid').hide();
                    $('.fade').hide();
                });

The main problema is when I call another modal within ajax, then I click in link, loading ajax appears, request is send to server, response returns, modal with data from request appears, but loading modal not hide.主要问题是当我在 ajax 中调用另一个模态时,然后单击链接,出现加载 ajax,请求发送到服务器,响应返回,出现来自请求的数据的模态,但加载模态不隐藏。

How can I fix it?我该如何解决?

Can you try binding to the ajax events like this: 您可以尝试像这样绑定到ajax事件吗?

$("#myid").bind({
   ajaxStart: function() { $(this).show(); },
   ajaxStop: function() { $(this).hide(); }
}); 

You have to set a timeout. 您必须设置一个超时时间。 That works for me: 这对我行得通:

setTimeout(function () { $("#myid").modal("hide"); }, 500);

Instead of using $('#myid').hide(); 而不是使用$('#myid').hide(); in ajaxStop, use $('#myid').modal('hide'); 在ajaxStop中,使用$('#myid').modal('hide');

Hide processing does not occur, probably because the closing is done without performing the modal opening operation.没有进行隐藏处理,可能是因为没有执行模态打开操作就完成了关闭。 This worked for me;这对我有用;

setTimeout(() => {$('#ModalExpeditionSeal').modal('hide')}, 350)

you can use as the following:你可以使用如下:

$("#myid").modal().hide() $("#myid").modal().hide()

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

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