Bootstrap模态框 - 当“调用操作”按钮被点击时关闭模态框

65

我在我的模态框中有一个外部链接,我想让模态框在用户单击该链接后隐藏。我该如何实现?

这是我的代码:

<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>
5个回答

130

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

假设你正在使用jQuery,你可以这样做:

$('#closemodal').click(function() {
    $('#modalwindow').modal('hide');
});
同样要确保在文档完成加载后绑定点击事件:
$(function() {
   // Place the above code inside this block
});
enter code here

4
modal 被附加到了一个 jQuery 对象上,因此你需要调用 $('.modal').modal() - Adam Hopkinson
1
很好的指出。修复中。虽然我必须说Kumar的答案更加优雅。 - jsalonen
是的!谢谢。需要将$('#closemodal').modal('hide');更改为$('#modalwindow').modal('hide');才能使其正常工作。 - Tomas Jacobsen
这是jQuery而不是普通的JavaScript? - mLstudent33
@webNoob13 很好的观点,是的,这是一个使用jQuery的解决方案。在当时回答这个问题的时候,Bootstrap非常流行,人们认为你总是会与它一起使用jQuery。我会更新答案,让它更加明确。 - jsalonen

17

移除你的脚本,并修改HTML:

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

编辑:请注意,目前此方法无法使用,因为在Bootstrap中尚不存在此功能。 在此处查看问题


3
是的,这将关闭模态框,但是新的目标链接并没有被打开。 - Tomas Jacobsen
如果这是一个外部链接,模态框是否关闭都无所谓,因为页面会重新加载。 - Adam Hopkinson
1
目标是"_blank",打开一个新窗口,而“旧”的窗口与模态框保持不变。 - Tomas Jacobsen
这个不起作用。在模态框内使用 data-dismiss="modal" 的链接不会跳转到它们的目标位置。https://github.com/twbs/bootstrap/issues/7192 - Louise Eggleton
@LouiseEggleton 这个答案太旧了。不管怎样,我应该做什么才能修改它,这样你就可以撤销我的踩和我的答案也变得正确? - Praveen Kumar Purushothaman
2
它并不算太旧。首先,答案是错误的,现在仍然是错误的。我给你的回答点了踩,因为它根本行不通,但是那些从谷歌搜索过来的人可能会看到它并尝试,浪费他们的时间。正如我在另一条评论中所说,“在模态框内使用 data-dismiss="modal" 的链接不会跳转到目标页面。”点击链接查看这个已知问题:https://github.com/twbs/bootstrap/issues/7192 正确的答案是被接受的答案。 - Louise Eggleton

7
使用data-dismiss="modal"。在我使用的Bootstrap版本v3.3.5中,当将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

0

按照所示进行操作。

  $(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>


0

我尝试使用加载了Bootstrap CSS的模态窗口进行关闭。但是close()方法并不能真正地关闭模态窗口。因此,我将display样式添加为"none"。

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

HTML代码将一个按钮嵌入对话框窗口中。
<input type="submit" value="Confirm" onclick="closeDialog()"/>

编辑:将 #closemodal 替换为 #modalwindow

<script type="text/javascript">
    $('#closemodal').click(function(e) {
       $('#modalwindow').modal('hide');
    }
</script>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接