简体   繁体   English

如何在jQuery中弹出对话框?

[英]How to get dialog to popup in jQuery?

I'm trying to get a dialog to popup when a button is clicked but nothing is happening.我试图在单击按钮时弹出一个对话框,但没有任何反应。 Here is the html:这是html:

<button id = "AddSymbol" class="btn" data-toggle="modal" data-target="#exampleModal">
   <i class="glyphicon glyphicon-plus-sign"></i> Syms
</button>

which creates a button that looks like this:这将创建一个如下所示的按钮: 在此处输入图片说明

But when I click it, nothing happens.但是当我点击它时,什么也没有发生。 No errors, nothing.没有错误,什么都没有。

Here is the html for the popup dialog which follows immediately after the above:以下是紧接在上述内容之后的弹出对话框的 html:

<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
     <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title" id="exampleModalLabel">Add Symbol To Watchlist</h5>
          <button type="button" class="close" data-dismiss="modal">
             <span aria-hidden="true">&times;</span>
          </button>
        </div>
        <div class="modal-body">
           <form>
             <div class="form-group">
               <label for="symbol" class="col-form-label">Symbol</label>
               <input type="text" class="form-control" id="symbol"></input>
             </div>
           </form>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-primary">Add</button>
        </div>
      </div>
    </div>
</div>   

Here is the javascript that is supposed to handle the click and show the popup:这是应该处理点击并显示弹出窗口的javascript:

$('#exampleModal').on('show.bs.modal', function (event) {
    var button = $('#AddSymbol') // Button that triggered the modal
    var modal = $(this)
    modal.find('.modal-title').text('Add Symbol To Watchlist')
})    

Here are the libraries included right after above:以下是紧接在上面的库:

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-blah-blah" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-blah-blah" crossorigin="anonymous"></script>

I also have this version of jQuery in the head section:我在 head 部分也有这个版本的 jQuery:

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-blah-blah" crossorigin="anonymous"></script>

If I move it to just above the other two like the instructions say, then errors occur from other js objects that need different versions.如果我按照说明将其移动到其他两个正上方,那么其他需要不同版本的 js 对象就会发生错误。 Here are the links in the head section for reference.以下是头部部分的链接供参考。 This is the only order that shows the page without error:这是显示页面没有错误的唯一顺序:

<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Gridsplit Main Example</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<link rel="stylesheet" href="C:\Users\Greg\Projects\JsonImporter\moneymachine\src\css\jquery.gridsplit.css">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="./dist/jquery.gridsplit.optimised.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.min.js" charset="utf-8"></script>
<link href="C:/Users/Greg/Projects/nvd3/build/nv.d3.css" rel="stylesheet" type="text/css">
<script src="C:/Users/Greg/Projects/nvd3/build/nv.d3.js"></script>
<link rel="stylesheet" type="text/css" href="C:\Users\Greg\Projects\JsonImporter\colResizable-1.6\samples\colResizable.Demo\css\main.css" />  

Here is a picture showing part of the page.这是显示页面的一部分的图片。 There are a couple other components - one for creating the adjustable layout dividers and one for creating the table with adjustable column widths.还有一些其他组件 - 一个用于创建可调整的布局分隔线,一个用于创建具有可调整列宽的表格。 They both can use the same version of jquery which doesn't work for the popup.他们都可以使用对弹出窗口不起作用的相同版本的 jquery。

在此处输入图片说明

I'm following the popup example in the section called "Varying Modal Content".我正在关注名为“不同模态内容”的部分中的弹出示例

I'm thinking the problem is that the other versions of jQuery are overriding the one that the popup needs, but I'm just guessing.我认为问题在于其他版本的 jQuery 覆盖了弹出窗口所需的版本,但我只是猜测。 Hopefully one of you experts can point out my error.希望你们中的一位专家可以指出我的错误。

How do you guys handle situations where you have multiple components on a web page that depend on specific versions of jQuery that interfere with each other?你们如何处理网页上有多个组件依赖于特定版本的 jQuery 相互干扰的情况?

If you aren't doing anything functional when the modal is shown and you just need to show the modal on button click.如果在显示模态时您没有做任何功能,并且您只需要在单击按钮时显示模态。

<button data-toggle="modal" data-target="#exampleModal">

If you want to handle it in javascript如果你想在 javascript 中处理它

$('#AddSymbol').on('click', function(e){
    $('#exampleModal').modal('show')
})

I hope its helps you:我希望它可以帮助您:

 $('#AddSymbol').on('click', function(e){ $('#myModal').modal('show') })
 <!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/4.5.2/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <h2>Modal Example</h2> <!-- Button to Open the Modal --> <button type="button" id="AddSymbol" class="btn btn-primary" > Open modal </button> <!-- The Modal --> <div class="modal" id="myModal"> <div class="modal-dialog"> <div class="modal-content"> <!-- Modal Header --> <div class="modal-header"> <h4 class="modal-title">Modal Heading</h4> <button type="button" class="close" data-dismiss="modal">&times;</button> </div> <!-- Modal body --> <div class="modal-body"> Modal body.. </div> <!-- Modal footer --> <div class="modal-footer"> <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button> </div> </div> </div> </div> </div> </body> </html>

使用 Bootstrap模型,这件事非常简单 检查上面的链接,您绝对可以从那里获得解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM