简体   繁体   English

模态弹出窗口在jQuery中不起作用

[英]Modal popup is not working in jquery

I have a submit button in a form ex: 我在表单ex中有一个提交按钮:

<div>
<form method = 'POST' action="#">
<input type="submit" class="btn btn-success myDialog" value="buy now"/>
</form>
</div>

I need to show pop-up dialog box when submit event is raised. submit事件发生时,我需要显示一个弹出对话框。 I tried somthing like this 我试过这样的东西

in html html

<div class="dialog">hello dialog box</div>

in .js file .js文件中

$( ".myDialog" ).click(function() {
   alert();// alert is working
  $( ".dialog" ).dialog({
  autoOpen: false,
  show: {
    effect: "blind",
    },
  hide: {
    effect: "explode",
       }
   });
});

for me the above code is not working even I tried dialog("open") but no use, can anyone help me on this. 对我来说,即使我尝试使用dialog("open") ,以上代码也无法正常工作,但没有用,任何人都可以帮我解决这个问题。

You will need to set autoOpen to true : 您将需要将autoOpen设置为true

  $(".dialog").dialog({
    autoOpen: true, // <-- set to true, which is default
    show: {
      ...
    ...
  });

You should also change from a click event on the button to a submit event on the form: 您还应该从按钮上的单击事件更改为表单上的提交事件:

$(".myDialog").closest("form").on("submit", function() {
  $(".dialog").dialog({
    show: {
      effect: "blind",
    },
    hide: {
      effect: "explode",
    }
  });
});

Alternative you can open it later with: 或者,您可以稍后使用以下方法打开它:

$(".dialog").dialog("open");

Try this.. 尝试这个..

$(".myDialog").click(function() {
  $(".dialog").dialog({
    show: {
      effect: "blind"
    },
    hide: {
      effect: "explode"
    }
  });
});

 $(".myDialog").click(function() { $(".dialog").dialog({ show: { effect: "blind" }, hide: { effect: "explode" } }); }); 
 <link rel="stylesheet" href="https://cdn.jsdelivr.net/bootstrap/3.3.5/css/bootstrap.min.css" /> <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> <link rel="stylesheet" href="https://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <button class="myDialog">open the dialog</button> <div class="dialog" title="Dialog Title" style="display: none">hello dialog box</div> 

<input type="button" class="btn btn-success myDialog" value="buy now"/>

Change the type of an input box. 更改输入框的类型。 Use "button" instead of "submit" and try. 使用“按钮”代替“提交”,然后尝试。

Use this. 用这个。

$( ".myDialog" ).click(function() {
  $( ".dialog" ).dialog({
  show: {
    effect: "blind",
    },
  hide: {
    effect: "explode",
       }
   });
});

and change type="submit" to type="button" 并将type="submit"更改为type="button"

<input type="buttom" class="btn btn-success myDialog" value="buy now"/>

This piece of code works.Change the input type from submit to button. 这段代码有效。将输入类型从提交更改为按钮。

 $(function() { $( ".dialog" ).dialog({ autoOpen: false, show: { effect: "blind", }, hide: { effect: "explode", } }); $( ".myDialog" ).click(function(e) { $(".dialog").dialog('open'); }); }) 
 <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script src="http://code.jquery.com/ui/1.11.1/jquery-ui.min.js"></script> <link rel="stylesheet" href="https://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css" /> <div> <form method = 'POST' action="#"> <input type="button" class="btn btn-success myDialog" value="buy now"/> </form> </div> <div class="dialog" hidden="hidden">hello dialog box</div> 

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

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