最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I execute some code if jQuery dialog was closed on escape? - Stack Overflow

programmeradmin1浏览0评论

I have a jQuery dialog and I need to execute some_code() when I press Esc or click the Cancel button. The latter is easy to implement, I just add define a property of the buttons object:

$('#mydialog').dialog({

  closeOnEscape: true,

  close: function(event, ui) {
    //some_code();
    $(this).dialog('destroy')
  },

  buttons: {
    "OK": function() {
      $(this).dialog("close");
    },
    "Cancel": function() {
      some_code();
      $(this).dialog("close");
    }
  }
});

But how can I execute some_code() after pressing Esc? This function must not be called clicking the OK button, so I cannot simply place it in the close event.

I have a jQuery dialog and I need to execute some_code() when I press Esc or click the Cancel button. The latter is easy to implement, I just add define a property of the buttons object:

$('#mydialog').dialog({

  closeOnEscape: true,

  close: function(event, ui) {
    //some_code();
    $(this).dialog('destroy')
  },

  buttons: {
    "OK": function() {
      $(this).dialog("close");
    },
    "Cancel": function() {
      some_code();
      $(this).dialog("close");
    }
  }
});

But how can I execute some_code() after pressing Esc? This function must not be called clicking the OK button, so I cannot simply place it in the close event.

Share Improve this question edited Jan 8, 2022 at 12:41 double-beep 5,53719 gold badges40 silver badges49 bronze badges asked Jan 19, 2014 at 15:59 VickelVickel 8,0076 gold badges38 silver badges62 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 13 +500

After googling around without finding an answer, I started looking into close: function(event, ui) part and found the solution using event.originalEvent (JSFiddle):

close: function(event, ui) {
  // some_code();
  if (event.originalEvent) {
    // triggered by clicking on dialog box X or pressing ESC
    // not triggered if a dialog button was clicked
    some_code();
  }
  $(this).dialog('destroy')
}
发布评论

评论列表(0)

  1. 暂无评论