【问题标题】:Bootstrap modal - close modal when "call to action" button is clicked引导模式 - 单击“号召性用语”按钮时关闭模式
【发布时间】:2012-10-06 14:00:40
【问题描述】:

我的模态框内有一个外部链接,我希望在用户单击该链接后隐藏模态框。我该怎么做?

这是我的代码:

<div class="modal hide fade" id="modalwindow">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3>Title</h3>
  </div>
  <div class="modal-body">
    <p>You need to do a search on google.com for that.</p>
  </div>
  <div class="modal-footer">
    <a id="closemodal" href="https://www.google.com" class="btn btn-primary" target="_blank">Launch google.com</a>
  </div>
</div>

<script type="text/javascript">
    $('#closemodal').modal('hide');
</script>

【问题讨论】:

    标签: javascript twitter-bootstrap


    【解决方案1】:

    您需要将模态隐藏调用绑定到onclick 事件。

    假设你使用的是 jQuery,你可以这样做:

    $('#closemodal').click(function() {
        $('#modalwindow').modal('hide');
    });
    

    还要确保在文档加载完成后绑定点击事件:

    $(function() {
       // Place the above code inside this block
    });
    enter code here
    

    【讨论】:

    • modal 附加到一个 jQuery 对象,所以你需要调用$('.modal').modal()
    • 说得好。定影。虽然我不得不说库马尔的回答要优雅得多。
    • 是的!谢谢你。需要更改 $('#closemodal').modal('hide');到 $('#modalwindow').modal('hide');让它发挥作用。
    • 这是 jquery 而不是 vanilla javascript?
    • @webNoob13 好点,是的,它是 jQuery 的解决方案。早在这个问题得到解答的那一天,jQuery 在 Bootstrap 中非常流行,以至于人们假设你总是会使用它。我会更新答案以更明确地说明这一点。
    【解决方案2】:

    删除您的脚本,并更改​​ HTML:

    <a id="closemodal" href="https://www.google.com" class="btn btn-primary close" data-dismiss="modal" target="_blank">Launch google.com</a>
    

    编辑:请注意,目前这将不起作用,因为此功能在引导程序中尚不存在。 See issue here.

    【讨论】:

    • 是的,这将关闭模式,但新的目标链接不会打开。
    • 如果是外部链接,那么模态是否关闭肯定没关系,因为页面会重新加载?
    • 目标是“_blank”,新窗口打开,模态的“旧”窗口保持不变。
    • 这不起作用。模态中使用 data-dismiss="modal" 的链接不会到达目的地。 github.com/twbs/bootstrap/issues/7192
    • 还不算太旧。首先,当时的答案是错误的,现在也是错误的。我投了反对票,因为你的答案不起作用,但是从谷歌停下来的人可能会看到它并尝试它并浪费他们的时间。正如我在其他评论中所说。 “使用 data-dismiss="modal" 的模式内的链接不会到达目的地。”转到链接以查看这是一个已知问题。 github.com/twbs/bootstrap/issues/7192 正确答案是接受的答案。
    【解决方案3】:

    使用data-dismiss="modal"。在我使用 v3.3.5 的 Bootstrap 版本中,当data-dismiss="modal" 被添加到如下所示的所需按钮时,它会漂亮地调用我的外部 Javascript (JQuery) 函数并神奇地关闭模式。太好了,我担心我不得不在另一个函数中调用一些模态隐藏并将其链接到真正的工作函数

     <a href="#" id="btnReleaseAll" class="btn btn-primary btn-default btn-small margin-right pull-right" data-dismiss="modal">Yes</a>
    

    在一些外部脚本文件中,在我准备好的文档中,当然有一个用于单击该标识符 ID 的功能

     $("#divExamListHeader").on('click', '#btnReleaseAll', function () {
                   // Do DatabaseMagic Here for a call a MVC ActionResult
    

    【讨论】:

      【解决方案4】:

      如图所示制作。

        $(document).ready(function(){
          $('#myModal').modal('show');
      
          $('#myBtn').on('click', function(){
            $('#myModal').modal('show');
          });
          
        });
      <br/>
      
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <!DOCTYPE html>
      <html lang="en">
      <head>
        <title>Bootstrap Example</title>
        <meta charset="utf-8">
        <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>
      </head>
      <body>
      
      <div class="container">
        <h2>Activate Modal with JavaScript</h2>
        <!-- Trigger the modal with a button -->
        <button type="button" class="btn btn-info btn-lg" id="myBtn">Open Modal</button>
      
        <!-- Modal -->
        <div class="modal fade" id="myModal" role="dialog">
          <div class="modal-dialog">
          
            <!-- Modal content-->
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Modal Header</h4>
              </div>
              <div class="modal-body">
                <p>Some text in the modal.</p>
              </div>
      
            </div>
            
          </div>
        </div>
        
      </div>

      【讨论】:

        【解决方案5】:

        我尝试关闭一个加载了引导 CSS 的模式窗口。 close() 方法并没有真正关闭模态窗口。所以我将显示样式添加为“none”。

            function closeDialog() {
                let d = document.getElementById('d')
                d.style.display = "none"
                d.close()
            }
        

        HTML 代码在对话框窗口中包含一个按钮。

        <input type="submit" value="Confirm" onclick="closeDialog()"/>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-12-26
          • 1970-01-01
          • 1970-01-01
          • 2016-01-12
          • 1970-01-01
          • 2015-03-08
          • 1970-01-01
          相关资源
          最近更新 更多