Bootstrap模态 - 单击“call to action”按钮时关闭模态

时间:2012-10-16 12:55:52

标签: javascript twitter-bootstrap

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

这是我的代码:

<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 search on google 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 个答案:

答案 0 :(得分:83)

您需要将模态隐藏调用绑定到click事件:

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

还要确保在文档加载完毕后绑定了click事件:

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

答案 1 :(得分:12)

删除您的脚本,然后更改HTML:

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

编辑:请注意,目前这不起作用,因为bootstrap中尚不存在此功能。 See issue here

答案 2 :(得分:3)

使用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>

在某些外部脚本文件中,在我的doc中,当然有点击该标识符ID的功能

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

答案 3 :(得分:0)

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

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

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

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

答案 4 :(得分:-1)

如图所示。

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