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

时间:2022-03-11 07:36:25

I'm using Twitter Bootstrap modal window functionality. When someone clicks submit on my form, I want to show the modal window upon clicking the "submit button" in the form.

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

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

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

10 个解决方案

#1


975  

Bootstrap has a few functions that can be called manually on modals:

Bootstrap有一些功能可以在modals上手动调用:

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

You can see more here: Bootstrap modal component

您可以在这里看到更多:引导模式组件

Specifically the methods section.

具体方法部分。

So you would need to change:

所以你需要改变:

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

to:

:

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

If you're looking to make a custom popup of your own, here's a suggested video from another community member:

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

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

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

#2


65  

In addition you can use via data attribute

此外,还可以使用data属性

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

In this particular case you don't need to use javascript.

在这种情况下,不需要使用javascript。

You can see more here: http://getbootstrap.com/2.3.2/javascript.html#modals

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

#3


46  

Most often, when $('#myModal').modal('show'); doesn't work, it's caused by having included jQuery twice. Including jQuery 2 times makes modals not to work.

通常,当美元(# myModal).modal(显示);不起作用,它是由于包含了两次jQuery而导致的。包括jQuery 2次使modals无效。

Remove one of the links to make it work again.

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

Furthermore, some plugins cause errors too, in this case add

此外,有些插件也会导致错误,在本例中是add

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

#4


12  

Just call the modal method(without passing any parameters) using jQuery selector.

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

Here is example:

下面是例子:

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

#5


9  

If you use links's onclick function to call a modal by jQuery, the "href" can't be null.

如果使用链接的onclick函数调用jQuery模态,“href”不能为空。

For example:

例如:

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

function openModal(){

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

The Modal can't show. The right code is :

模态不能显示。正确的代码是:

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

#6


8  

Check out the complete solution here:

查看完整的解决方案:

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

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

Make sure to put libraries in required order to get result:

确保将库放在必需的位置以获得结果:

1- First bootstrap.min.css 2- jquery.min.js 3- bootstrap.min.js

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

(In other words jquery.min.js must be call before bootstrap.min.js)

(换句话说jquery.min。必须在bootstrap.min.js之前调用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>

#7


5  

I tried several methods which failed, but the below worked for me like a charm:

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

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

#8


5  

Here is how to load bootstrap alert as soon as the document is ready. It is very easy just add

以下是如何在文档准备好后立即加载引导警报的方法。很容易添加

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

I made a demo on W3Schools.

我在W3Schools网站上做了一个演示。

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

#9


1  

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

You can launch the modal with the code below this.

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

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

#10


1  

Try

试一试

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

To open or close the modal with id myModal.

用myid模式打开或关闭模式。

If the above is not working then it means bootstrap.js has been overridden by some other js file. Here is a solution

如果上面的方法不起作用,那么它意味着引导。js已被其他一些js文件覆盖。这是一个解决方案

1:- Move bootstrap.js to the bottom so that it will override other js files.

1:引导。到底部的js,以便它将覆盖其他js文件。

2:- Make sure the order is like below

2:-确保订单如下。

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

#1


975  

Bootstrap has a few functions that can be called manually on modals:

Bootstrap有一些功能可以在modals上手动调用:

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

You can see more here: Bootstrap modal component

您可以在这里看到更多:引导模式组件

Specifically the methods section.

具体方法部分。

So you would need to change:

所以你需要改变:

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

to:

:

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

If you're looking to make a custom popup of your own, here's a suggested video from another community member:

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

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

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

#2


65  

In addition you can use via data attribute

此外,还可以使用data属性

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

In this particular case you don't need to use javascript.

在这种情况下,不需要使用javascript。

You can see more here: http://getbootstrap.com/2.3.2/javascript.html#modals

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

#3


46  

Most often, when $('#myModal').modal('show'); doesn't work, it's caused by having included jQuery twice. Including jQuery 2 times makes modals not to work.

通常,当美元(# myModal).modal(显示);不起作用,它是由于包含了两次jQuery而导致的。包括jQuery 2次使modals无效。

Remove one of the links to make it work again.

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

Furthermore, some plugins cause errors too, in this case add

此外,有些插件也会导致错误,在本例中是add

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

#4


12  

Just call the modal method(without passing any parameters) using jQuery selector.

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

Here is example:

下面是例子:

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

#5


9  

If you use links's onclick function to call a modal by jQuery, the "href" can't be null.

如果使用链接的onclick函数调用jQuery模态,“href”不能为空。

For example:

例如:

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

function openModal(){

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

The Modal can't show. The right code is :

模态不能显示。正确的代码是:

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

#6


8  

Check out the complete solution here:

查看完整的解决方案:

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

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

Make sure to put libraries in required order to get result:

确保将库放在必需的位置以获得结果:

1- First bootstrap.min.css 2- jquery.min.js 3- bootstrap.min.js

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

(In other words jquery.min.js must be call before bootstrap.min.js)

(换句话说jquery.min。必须在bootstrap.min.js之前调用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>

#7


5  

I tried several methods which failed, but the below worked for me like a charm:

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

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

#8


5  

Here is how to load bootstrap alert as soon as the document is ready. It is very easy just add

以下是如何在文档准备好后立即加载引导警报的方法。很容易添加

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

I made a demo on W3Schools.

我在W3Schools网站上做了一个演示。

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

#9


1  

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

You can launch the modal with the code below this.

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

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

#10


1  

Try

试一试

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

To open or close the modal with id myModal.

用myid模式打开或关闭模式。

If the above is not working then it means bootstrap.js has been overridden by some other js file. Here is a solution

如果上面的方法不起作用,那么它意味着引导。js已被其他一些js文件覆盖。这是一个解决方案

1:- Move bootstrap.js to the bottom so that it will override other js files.

1:引导。到底部的js,以便它将覆盖其他js文件。

2:- Make sure the order is like below

2:-确保订单如下。

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