首页 / Bootstrap / 正文

[Bootstrap]Bootstrap中如何使用jQuery弹出一个模态窗口?

5717 发布于: 2018-01-25 读完约需11分钟

问题描述

如今有如下的HTML代码和jQuery操作,需求是在点击提交按钮时弹出一个模态窗口,代码如下:
HTML:

<form id="myform" class="form-wizard">
    <h2 class="form-wizard-heading">BootStap Wizzard 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();
});

方案一

Bootstrap 中,提供了模态窗口的一些方法,其中包括:toggle,show,hide

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

所以,上面的 jQuery 弹出 Bootstrap 模态窗口操作应该作一点微小的修改。

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

修改成:

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

方案二

使用 a 标签可以直接打开一个 Bootstrap 模态窗口(特别注意:a标签的href属性不能为空),如:

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

方案三

一个简单但完整的Bootstrap打开模态窗口的示例(提示:复制以下代码并保存到本地的任意一个html文件,然后打开即可查看示例):

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

版权声明:本作品系原创,版权归码友网所有,如未经许可,禁止任何形式转载,违者必究。

上一篇: [.NET/C#].NET程序开发中怎么使用反射通过一个类型获取对应的实例对象?

下一篇: [.NET/C#]分享一个C#日期时间静态扩展类

本文永久链接码友网 » [Bootstrap]Bootstrap中如何使用jQuery弹出一个模态窗口?

分享扩散:

发表评论

登录用户才能发表评论, 请 登 录 或者 注册