我正在使用Twitter引导模态对话框。当我点击引导模式对话框的提交按钮时,它会发送一个AJAX请求。我的问题是情态背景并没有消失。模态对话框确实消失了,但是“模态背景”在屏幕上创建的不透明度仍然存在

我该怎么办?


当前回答

$ (' # myModal ') .trigger(“点击”);

这对我很管用。它没有关闭,因为当你打开弹出窗口时,它会触发2或3个模式背景。当你执行$('#myModal')).modal('hide');它只影响一个情态-背景和其余的。

其他回答

use:

$('.modal.in').modal('hide') 

甚至我也遇到了类似的问题,我有以下两个按钮

<button id="confirm-delete-btn" >Yes, Delete this note.</button>
<button id="confirm-delete-cancel" data-dismiss="modal">No</button>

我想执行一些ajax操作和成功的ajax操作关闭模式。这就是我所做的。

$.ajax({
        url: '/ABC/Delete/' + self.model.get("Id")
            , type: 'DELETE'
            , success: function () {                    
                setTimeout(function () {
                    self.$("#confirm-delete-cancel").trigger("click");
                }, 1200);
            }
            , error: function () {}
        });

我触发了“No”按钮的点击事件,该按钮具有data-dismiss="modal"属性。这是有效的:)

在动作按钮中插入:

data-backdrop="false"

and

data-dismiss="modal" 

例子:

<button type="button" class="btn btn-default" data-dismiss="modal">Done</button>

<button type="button" class="btn btn-danger danger" data-dismiss="modal" data-backdrop="false">Action</button>

如果你输入这个data-attr, .modal- background将不会出现。 关于它的文档在这个链接:http://getbootstrap.com/javascript/#modals-usage

我也遇到过类似的问题。我将Boostrap与Backbone结合使用,我捕捉到点击“Do it”按钮,然后停止这些事件的传播。奇怪的是,这让模态消失了,但背景却没有。如果我调用event.preventDefault(),但不调用stopPropagation(),这一切都很好。

在考虑了所有的答案之后,我想出了一个包罗万象的解决方案。 最终,这是唯一对我有用的方法。 (香草javascript):

            var elem = document.querySelectorAll('[data-dismiss="modal"]')[0];
            var evt = new Event('click');
            elem.dispatchEvent(evt);

            var modalelem = document.getElementById('exampleModalCenter');

            var myModal = new bootstrap.Modal(modalelem, { keyboard: false })

            const fade = modalelem.classList.contains('fade');
            if (fade) {
                modalelem.classList.remove('fade');
            }

            myModal.hide();
            myModal.dispose();

            var backdrop = document.getElementsByClassName('modal-backdrop')[0];
            backdrop.style.display = 'none';

            var modalopen = document.getElementsByClassName('modal-open')[0];
            modalopen.classList.remove('modal-open');
            modalopen.style.removeProperty("overflow");\