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

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

当前回答

Try

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

其他回答

试试这个

myModal1是modal的id

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

此外,您还可以使用via data属性

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

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

你可以在这里看到更多:http://getbootstrap.com/2.3.2/javascript.html#modals

点击这里查看完整的解决方案:

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>

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

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

你可以在这里看到更多:Bootstrap模态组件

特别是方法部分。

所以你需要改变:

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

to:

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

如果你想制作一个自己的自定义弹出窗口,这里有一个来自其他社区成员的建议视频:

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

如果你使用链接的onclick函数调用jQuery的模态,“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>