简体   繁体   English

Bootstrap 模式 - 单击“号召性用语”按钮时关闭模式

[英]Bootstrap modal - close modal when “call to action” button is clicked

I have a external link inside my modal, and I want the modal to hide after the user has clicked on the link.我的模态中有一个外部链接,我希望在用户单击链接后隐藏模态。 How do I do that?我怎么做?

Here is my code:这是我的代码:

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

You need to bind the modal hide call to the onclick event.您需要将模态隐藏调用绑定到onclick事件。

Assuming you are using jQuery you can do that with:假设您正在使用 jQuery,您可以使用以下方法:

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

Also make sure the click event is bound after the document has finished loading:还要确保在文档加载完成后绑定了 click 事件:

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

Remove your script, and change the HTML:删除您的脚本,并更改​​ HTML:

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

EDIT: Please note that currently this will not work as this functionality does not yet exist in bootstrap.编辑:请注意,目前这不起作用,因为引导程序中尚不存在此功能。 See issue here .请参阅此处的问题

Use data-dismiss="modal" .使用data-dismiss="modal" In the version of Bootstrap I am using v3.3.5, when data-dismiss="modal" is added to the desired button like shown below it calls my external Javascript (JQuery) function beautifully and magically closes the modal.在 Bootstrap 的版本中,我使用的是 v3.3.5,当data-dismiss="modal"添加到所需的按钮时,如下所示,它会漂亮地调用我的外部 Javascript (JQuery) 函数并神奇地关闭模态。 Its soo Sweet, I was worried I would have to call some modal hide in another function and chain that to the real working function它太甜了,我担心我必须在另一个函数中调用一些模态隐藏并将其链接到真正的工作函数

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

In some external script file, and in my doc ready there is of course a function for the click of that identifier ID在一些外部脚本文件中,在我准备好的文档中,当然有一个用于单击该标识符 ID 的函数

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

Make as shown.如图制作。

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

I tried closing a modal window with a bootstrap CSS loaded.我尝试关闭加载了引导 CSS 的模式窗口。 The close () method does not really close the modal window. close() 方法并没有真正关闭模态窗口。 So I added the display style to "none".所以我将显示样式添加为“无”。

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

The HTML code includes a button into the dialog window. HTML 代码在对话框窗口中包含一个按钮。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM