【问题标题】:How to open a Bootstrap modal using jQuery?如何使用 jQuery 打开 Bootstrap 模式?
【发布时间】:2019-12-28 02:28:47
【问题描述】:

我正在使用 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>

$('#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();
});

我有这个网站的检查演示 https://bootstrapmodal.com

【问题讨论】:

  • 您的模态 ID 不匹配

标签: bootstrap-modal


【解决方案1】:

我在下面包含的模态中缺少一些 div(模态对话框和模态内容类)

还为您的输入添加了一个名称属性,以便您可以根据https://api.jquery.com/serialize/使用 serialize() 对其进行序列化

$('#myform').on('submit', function(ev) {
    $('#myModal').modal('show');


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

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

    ev.preventDefault();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.0/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-SI27wrMjH3ZZ89r4o+fGIJtnzkAnFs3E4qz9DIYioCQ5l9Rd/7UAa8DHcaL8jkWt" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.0/js/bootstrap.min.js" integrity="sha384-3qaqj0lc6sV/qpzrc1N5DC6i1VRn/HyX4qdPaiEFbn54VjQBEU341pvjz7Dv3n6P" crossorigin="anonymous"></script>

<form id="myform" class="form-wizard">
    <h2 class="form-wizard-heading">BootStap Wizard Form</h2>
    <input type="text" name="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-dialog" role="document">
  <div class="modal-content">
    <div class="modal-header">
      <h3 id="myModalLabel">Modal header</h3>
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
        
    </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>
    </div>
</div>

【讨论】:

    猜你喜欢
    • 2020-06-26
    • 1970-01-01
    • 2017-09-07
    • 2023-04-06
    • 2016-04-02
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    • 2012-05-03
    相关资源
    最近更新 更多