简体   繁体   English

一键关闭后如何使jquery对话框关闭

[英]How to get jquery dialog box to close after one click

My dialog box only closes after two clicks and not one. 我的对话框仅在单击两次后关闭,而不是单击一次。 I am not sure why it won't close on the first button click. 我不确定为什么第一次单击时它不会关闭。 Does the dialog box need to be hidden? 对话框是否需要隐藏? I tried dialog.dialog.hide(); 我试过dialog.dialog.hide(); as well right after close, but that gives me no luck. 收盘后也是如此,但这并没有给我带来好运。 This is what I have for my dialog. 这就是我的对话框。

var dialog = $('<p>Cannot post. </p>').dialog({
  height: 150,
  width: 300,
  buttons: {
    "Ok": function(event) {
      event.preventDefault();
      dialog.dialog('close');
      $(this).display = 'none';
    }
  }
});

Consider the following: https://jsfiddle.net/Twisty/eo4z5gaj/ 考虑以下内容: https : //jsfiddle.net/Twisty/eo4z5gaj/

JavaScript JavaScript的

$(function() {
  var dialog = $('<p>Cannot post. </p>').dialog({
    height: 150,
    width: 300,
    buttons: {
      "Ok": function(event) {
        $(this).dialog('close');
      }
    }
  });
});

It's better to refernce $(this) for .dialog() . 最好为.dialog()引用$(this) .dialog() Essentially they are the same. 本质上它们是相同的。

If this code is still requiring two clicks of "Ok", then you should look at your browser or console. 如果此代码仍需要单击两次“确定”,则应查看浏览器或控制台。 Code above works with single click in FireFox. 上面的代码只需在FireFox中单击即可工作。

You may also consider: https://jsfiddle.net/Twisty/eo4z5gaj/8/ 您还可以考虑: https : //jsfiddle.net/Twisty/eo4z5gaj/8/

JavaScript JavaScript的

$(function() {
  var dialog = $('<div>', {
    title: "Error"
  }).html("<p>Cannot Post.</p>").dialog({
    height: 160,
    width: 300,
    buttons: {
      "Ok": function(event) {
        $(this).dialog('close');
      }
    }
  });
});

Hope that helps. 希望有所帮助。

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

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