如何检测到jQuery UI对话框的(X)关闭按钮是否与dialogclose / dialogbeforeclose事件分开单击?

我想能够检测到被点击的jQuery UI对话的(x)关闭按钮,但是我不想使用dialogclose / dialogbeforeclose事件(因为我相信这些将会触发,无论对话框如何关闭).

我试过$(“.ui-dialog-titlebar-close”).live(“click”),但这似乎不起作用.

我该怎么做?

示例代码:(当对话关闭时,调试器不会启动).

<!DOCTYPE html>
<html>
<head>
  <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.js"></script>

  <script>
  $(document).ready(function() {
    $("#dialog").dialog();

    $(".ui-dialog-titlebar-close").live("click", function() { 
        debugger; // ** clicking the close button doesn't get to here.**
        });
  });
  </script>
</head>

<div id="dialog" title="Dialog Title">I'm in a dialog</div>

</body>
</html>
您可以完全按照JAAulde的建议,或者避免跟踪绑定并使用create事件:

$(document).ready(function() {
    $('#dialog').dialog({
        create: function() {
            $(this).closest('div.ui-dialog')
                   .find('.ui-dialog-titlebar-close')
                   .click(function(e) {
                       alert('hi');
                       e.preventDefault();
                   });
        }
    });
});
翻译自:https://stackoverflow.com/questions/7924152/how-can-i-detect-that-the-x-close-button-of-a-jquery-ui-dialog-was-clicked-se

转载注明原文:如何检测到jQuery UI对话框的(X)关闭按钮是否与dialogclose / dialogbeforeclose事件分开单击?