简体   繁体   中英

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

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() . 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.

You may also consider: https://jsfiddle.net/Twisty/eo4z5gaj/8/

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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