如何使用 jQuery 打开 Bootstrap 模式窗口?

问题描述 投票:0回答:16

我正在使用 Twitter Bootstrap 模式窗口功能。当有人单击我的表单上的“提交”时,我想在单击表单中的“提交按钮”时显示模式窗口。

<form id="myform" class="form-wizard">
    <h2 class="form-wizard-heading">BootStap Wizard Form</h2>
    <input type="text" value=""/>
    <input type="submit"/>
</form>

<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="myModalLabel">Modal header</h3>
    </div>
    <div class="modal-body">
        <p>One fine body…</p>
    </div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
        <button class="btn btn-primary">Save changes</button>
    </div>
</div>

jQuery:

$('#myform').on('submit', function(ev) {
    $('#my-modal').modal({
        show: 'false'
    }); 


    var data = $(this).serializeObject();
    json_data = JSON.stringify(data);
    $("#results").text(json_data); 
    $(".modal-body").text(json_data); 

    // $("#results").text(data);

    ev.preventDefault();
});
javascript jquery twitter-bootstrap
16个回答
1781
投票

Bootstrap 有一些可以在模态上手动调用的函数:

$('#myModal').modal('toggle');
$('#myModal').modal('show');
$('#myModal').modal('hide');

您可以在此处查看更多内容:Bootstrap 模态组件

特别是方法部分

所以你需要改变:

$('#my-modal').modal({
    show: 'false'
}); 

至:

$('#myModal').modal('show'); 

如果您想制作自己的自定义弹出窗口,请观看另一位社区成员推荐的视频:

https://www.youtube.com/watch?v=zK4nXa84Km4


116
投票

大多数情况下,当

$('#myModal').modal('show');
不起作用时,这是由于两次包含 jQuery 造成的。包含 jQuery 2 次会使模态不起作用。

删除其中一个链接即可使其再次工作。

此外,某些插件也会导致错误,在这种情况下添加

jQuery.noConflict(); 
$('#myModal').modal('show'); 

101
投票

此外您还可以通过数据属性使用

<button type="button" data-toggle="modal" data-target="#myModal">Launch modal</button>

在这种特殊情况下,您不需要编写 javascript。

您可以在此处查看更多信息:http://getbootstrap.com/2.3.2/javascript.html#modals


25
投票

只需使用

jQuery
选择器调用模态方法(不传递任何参数)。

这是示例:

$('#modal').modal();

19
投票

如果您使用链接的 onclick 函数通过 jQuery 调用模态框,则“href”不能为 null。

例如:

... ...
<a href="" onclick="openModal()">Open a Modal by jQuery</a>
... ...
... ...
<script type="text/javascript">

function openModal(){

    $('#myModal').modal();
}       
</script>

模态框无法显示。 正确的代码是:

<a href="#" onclick="openModal()">Open a Modal by jQuery</a>

19
投票
<script type="text/javascript">
    $(function () {
        $("mybtn").click(function () {
            $("#my-modal").modal("show");
        });
    });
</script>

17
投票

以下是如何在文档准备好后立即加载引导程序警报。非常简单,只需添加

 $(document).ready(function(){
   $("#myModal").modal();
});

我在 W3Schools 上做了一个 demo

<!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.2.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>Here is how to load a bootstrap modal as soon as the document is ready </h2>
  <!-- Trigger the modal with a 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 class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        </div>
      </div>
      
    </div>
  </div>
  
</div>

<script>
$(document).ready(function(){
   $("#myModal").modal();
});
</script>

</body>
</html>


12
投票

在调用函数时尝试使用 jQuery 而不是 $ 符号,它在我的例子中有效,如下所示。

jQuery.noConflict(); 
jQuery('#myModal').modal('show'); 

jQuery.noConflict(); 因为当 jQuery('#myModal').modal('show'); 时不起作用,这是由于两次包含 jQuery 造成的。包含 jQuery 2 次会使模式无法工作。在这种情况下它会解决问题,就像在我的情况下它正在重复一样。

您还可以访问此链接

通过 JQuery 调用无法显示 Bootstrap 模型窗口


11
投票

在此处查看完整的解决方案:

http://www.w3schools.com/bootstrap/tryit.asp?filename=trybs_ref_js_modal_show&stacked=h

确保按所需顺序放置库以获得结果:

1- 第一个 bootstrap.min.css 2- jquery.min.js 3- bootstrap.min.js

(也就是说 jquery.min.js 必须在 bootstrap.min.js 之前调用)

    <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.1.1/jquery.min.js">
</script> 

 <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

10
投票

我尝试了几种方法都失败了,但下面的方法对我来说就像一个魅力:

$('#myModal').appendTo("body").modal('show');

8
投票

尝试

$("#myModal").modal("toggle")

打开或关闭 ID 为 myModal 的模式。

如果上述方法不起作用,则意味着 bootstrap.js 已被其他 js 文件覆盖。 这里有一个解决方案

1:- 将 bootstrap.js 移至底部,以便它将覆盖其他 js 文件。

2:- 确保顺序如下

<script src="plugins/jQuery/jquery-2.2.3.min.js"></script>
<!-- Other js files -->
<script src="plugins/jQuery/bootstrap.min.js"></script>

5
投票
<form id="myform" class="form-wizard">
    <h2 class="form-wizard-heading">BootStap Wizard Form</h2>
    <input type="text" value=""/>
    <input type="submit" id="submitButton"/>
</form>

<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        <h3 id="myModalLabel">Modal header</h3>
    </div>
    <div class="modal-body">
        <p>One fine body…</p>
    </div>
    <div class="modal-footer">
        <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
        <button class="btn btn-primary">Save changes</button>
    </div>
</div>

您可以使用下面的代码启动模式。

$(document).ready(function(){
    $("#submitButton").click(function(){
        $("#myModal").modal();
    });
});

4
投票

试试这个

myModal1 是模态的 id

$('#myModal1').modal({ show: true });

3
投票

Bootstrap 4.3 - 更多这里

$('#exampleModal').modal();
<!-- Initialize Bootstrap 4 -->

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>


<!-- MODAL -->

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
    
      <div class="modal-body">
        Hello world   
      </div>
      
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>

    </div>
  </div>
</div>


0
投票

试试这个。这对我来说很有效。

function clicked(item) {
         alert($(item).attr("id"));
        }
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-rtl/3.4.0/css/bootstrap-rtl.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<button onclick="clicked(this);" id="modalME">Click me</button>

<!-- Modal -->
  <div class="modal" id="modalME" aria-hidden="true">
    <div class="modal-dialog">
      <div class="modal-header">
        <h2>Modal in CSS</h2>
        <a href="#close" class="btn-close" aria-hidden="true">×</a> <!--CHANGED TO "#close"-->
      </div>
      <div class="modal-body">
        <p>One modal example here.</p>
      </div>
      <div class="modal-footer">
        <a href="#close" class="btn">Nice!</a>  <!--CHANGED TO "#close"-->
      </div>
      </div>
    </div>
  </div>
  <!-- /Modal -->


0
投票

上述解决方案在 Bootstrap 4.5 中都不适合我。

这是我发现的确保从另一个模态打开的模态放置在顶部的最有效方法。

将其添加到您的 CSS 中:

 /* Use this for Stacked Modals in BS 4.5 */
 .modal:nth-of-type(even) {
     z-index: 1052 !important;
 }

 .modal-backdrop.show:nth-of-type(even) {
     z-index: 1051 !important;
 }

只需从 JQuery 调用您的模态: $('#modCommissionItemApplied').modal('show');

© www.soinside.com 2019 - 2024. All rights reserved.