【问题标题】:Second Bootstrap model opens automatically after first model close第一个模型关闭后,第二个 Bootstrap 模态会自动打开
【发布时间】:2018-04-13 13:06:16
【问题描述】:

这里是代码。

jQuery(document).ready(function() {
    $('body').on('click', '#saves', function(event){
	      event.preventDefault();
	      $('#exampleModal').modal('hide');
	      $("#exampleModal").on("hidden.bs.modal",function(){
	          $('#Newmodel').modal('show')
  	    });
    });
});
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Modal</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
        <link rel="stylesheet" href="">
    </head>
    <body>
        <!-- Button trigger modal -->
        <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
        Launch demo modal
        </button>
        <!-- Modal -->
        <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        ...
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <button type="button" id="saves" class="btn btn-primary">New Model</button>
                    </div>
                </div>
            </div>
        </div>
        <!-- Modal -->
        <div class="modal fade" id="Newmodel" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel1" aria-hidden="true">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        ...
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>

除了成功函数中还有 ajax 请求和 jquery 之外,我的代码与链接代码相同。在第一个模型中,如果我提交,那么它会触发 ajax 请求并在成功函数中,根据 jquery 代码,关闭第一个模态并打开带有消息的第二个模态。在第一次打开第二个模式时没有问题。

问题是当我第二次打开第一个模态时,然后如果我不想要像单击提交这样的 ajax 请求,发送数据并关闭现有模态并打开新模态。所以我点击关闭按钮或页面的任何外部区域。所以第二个模态不应该打开,因为第二个模态没有触发器。但它仍然关闭第一个模态并打开第二个模态。

我不知道为什么在第一次运行后会发生这种情况。我不想打开第二个模式,除了提交点击。我也尝试过不使用“hidden.bs.modal()”函数,但在该方法中,滚动条在模式以外的背景页面上工作。 如果没有背景滚动,我该如何做到这一点?

【问题讨论】:

    标签: jquery twitter-bootstrap bootstrap-modal


    【解决方案1】:

    您的问题似乎是这一直被触发

    $("#exampleModal").on("hidden.bs.modal",function() {

    我很好奇是否有链接事件的方法。就像如果你有一个 onclick 和 hidden.bs.modal 事件,做点什么。那是你想要做的吗?您拥有它的方式将始终在该条件下运行代码,尽管它在另一个事件条件内。

    以下内容按您希望的方式工作:

    jQuery(document).ready(function() {
        $('body').on('click', '#saves', function(event){
    	      event.preventDefault();
    	      $('#exampleModal').modal('hide');
    	      $('#Newmodel').modal('show');
        });
    });
    <!DOCTYPE html>
    <html>
        <head>
            <meta charset="utf-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <title>Modal</title>
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
            <link rel="stylesheet" href="">
        </head>
        <body>
            <!-- Button trigger modal -->
            <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">
            Launch demo modal
            </button>
            <!-- Modal -->
            <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body">
                            ...
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                            <button type="button" id="saves" class="btn btn-primary">New Model</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- Modal -->
            <div class="modal fade" id="Newmodel" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel1" aria-hidden="true">
                <div class="modal-dialog" role="document">
                    <div class="modal-content">
                        <div class="modal-header">
                            <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                            </button>
                        </div>
                        <div class="modal-body">
                            ...
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        </div>
                    </div>
                </div>
            </div>
        </body>
    </html>

    【讨论】:

    • 我也试过了。它工作正常。在此代码中,滚动适用于背景页面,我不希望这样。还有其他方法吗?
    • 您的意思是当您点击Run code sn-p 后在那个小窗口上查看时启用了滚动?如果是这样,可以使用.modal-open .modal { overflow: hidden; } 将其关闭。但是,如果您使用超出窗口大小的大量信息填充模态框,人们将无法滚动到它。如果您单击 sn-p 区域右上角的 整页 链接,您将在整页上看到它。那里没有滚动。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-29
    • 2016-04-11
    • 2014-10-08
    • 2021-09-12
    • 2017-03-06
    相关资源
    最近更新 更多