Bootstrap 3: 防止modal inside modal每次都触发(hidden.bs.modal)

Bootstrap 3: prevent modal inside modal to trigger (hidden.bs.modal) every time

我在另一个模态中有一个模态,我设法关闭了内部模态而不影响另一个。问题是,当第二个模态关闭时,它会为其自身和第一个模态触发 'hidden.bs.modal' 事件。

<!-- My Html -->
<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Open demo modal</button>
<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                 <h4 class="modal-title" id="myModalLabel">Demo Modal</h4>
            </div>
            <div class="modal-body">
                <p>
                    <a href="#" data-toggle="modal" data-target="#uploadImage" class="btn btn-primary">Upload image</a>
                </p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
    <div class="modal" id="uploadImage" tabindex="-1" role="dialog" aria-labelledby="uploadImage-title" aria-hidden="true">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                     <h4 class="modal-title" id="uploadImage-title">Upload new image</h4>
                </div>
                <div class="modal-body">
                    Testing Area
                </div>
                <div class="modal-footer">                
                    <button type="button" class="btn btn-default" id="btnUploadCancel">Cancel</button>
                    <button type="button" class="btn btn-success">Accept</button>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- My JS-->
$('#btnUploadCancel').click(function () {
    $('#uploadImage').modal('toggle');
});

$(document).on('hidden.bs.modal', '#myModal', function () {
    alert("hello");
});

$(document).on('hidden.bs.modal', '#uploadImage', function () {
    alert("goodbye");
});

这是我制作的jsFiddle example

这个问题的原因是我只需要在第二个模式被隐藏时触发一个动作,然后在第一个模式被隐藏时触发其他动作。 关于如何使用每个事件解决此问题的任何想法???

注意: 第二个模态框必须在另一个模态框内,因为它们被称为局部视图。

第二个模态 html 不必包含在第一个模态正文中。仅包含数据目标 link 并将第二个模态移到第一个模态之外。这样事件就会如您所愿地触发。

<button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">Open demo modal</button>
<div class="modal" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                 <h4 class="modal-title" id="myModalLabel">Demo Modal</h4>
            </div>
            <div class="modal-body">
                <p>
                    <a href="#" data-toggle="modal" data-target="#uploadImage" class="btn btn-primary">Upload image</a>
                </p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
        </div>
    </div>

</div>
<div class="modal" id="uploadImage" tabindex="-1" role="dialog" aria-labelledby="uploadImage-title" aria-hidden="true">
        <div class="modal-dialog modal-sm">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                     <h4 class="modal-title" id="uploadImage-title">Upload new image</h4>
                </div>
                <div class="modal-body">
                    Testing Area
                </div>
                <div class="modal-footer">                
                    <button type="button" class="btn btn-default" id="btnUploadCancel">Cancel</button>
                    <button type="button" class="btn btn-success">Accept</button>
                </div>
            </div>
        </div>
    </div>

http://jsfiddle.net/wytf57mL/3/

我猜这些模态元素是异步引入页面的,这就是为什么您使用绑定到文档的委托事件侦听器而不是直接绑定 hidden.bs.modal#myModal#uploadImage 他们自己。如果不是这种情况,并且您可以直接绑定,我建议使用这种方法在 #uploadImage 本身上捕获 hidden 事件,并防止它冒泡 DOM 树使用 event.stopPropagation();.

或者,您可以继续将此处理程序委托给文档,并使用传递到处理程序回调中的 Event 对象的 target 属性 来确定哪个元素是事件的实际来源:

$(document).on('hidden.bs.modal', '#myModal', function (event) {
    if (event.target.id == 'uploadImage') {
        // do stuff when the upload dialog is hidden.
    }
    else if (event.target.id == 'myModal') {
        // do stuff when the outer dialog is hidden.
    }
});

P.S.: 你可能已经知道,Bootstrap 框架 does not support overlapping modal dialogs。当您继续在模态中嵌套模态时,请注意这一点,尤其是关于通过标记 API 和 data-dismiss="modal".

初始化的解雇元素

P.P.S.: