简体   繁体   English

jQuery UI对话框中关闭(x)按钮的专用事件处理程序

[英]Dedicated event handler for close (x) button in jQuery UI Dialog

Is there a way to wire into the close (x) button on a jQuery UI Dialog, such that you can provide a dedicated event handler? 有没有办法连接到jQuery UI对话框上的close(x)按钮,这样你就可以提供专用的事件处理程序? Using the "close" or "beforeclose" event does not work because if you have other buttons in your dialog that also cause the dialog to close, you are always going to hit the "close" and "beforeclose" events, which is not desirable. 使用“close”或“beforeclose”事件不起作用,因为如果对话框中还有其他按钮也会导致对话框关闭,那么您总是会遇到“关闭”和“关闭前”事件,这是不可取的。 I want a way to run specific code from the close (x) button. 我想要一种从close(x)按钮运行特定代码的方法。

Whenever one event leads to another event inside a jQuery UI widget, the original event is always included in the event object. 每当一个事件导致jQuery UI小部件中的另一个事件时,原始事件总是包含在事件对象中。 In this case, you can look at the event object passed to the close callback or the dialogclose event and check if event.originalEvent exists. 在这种情况下,您可以查看传递给close回调或dialogclose事件的事件对象,并检查event.originalEvent存在。 If it does, then you can assume that the dialog was closed via a click on the close button. 如果是,则可以假设通过单击关闭按钮关闭对话框。 This also applies to beforeclose . 这也适用于beforeclose

If you want to be absolutely sure that it was the close button in the titlebar, then you can check event.originalEvent.target and either check the class or the DOM location using .closest() . 如果你想绝对确定它是标题栏中的关闭按钮,那么你可以检查event.originalEvent.target并使用.closest()检查类或DOM位置。

Here's a jsbin showing this in action: http://jsbin.com/ajoheWAB/1/edit 这里有一个jsbin显示了这个: http ://jsbin.com/ajoheWAB/1/edit

You could try: 你可以尝试:

$(document).on('click','.ui-dialog-titlebar-close',function(){
    //close button clicked
});

As far as I know there's no way to wire directly to that button, but you can do something specific to your popup by adding a dialogClass and wiring an event handler yourself: 据我所知,没有办法直接连接到该按钮,但您可以通过添加dialogClass并自行连接事件处理程序来为弹出窗口执行特定操作:

var dialogClass ="yourPopupTitle";

$(document).on('click', '.'+ dialogClass  +' .ui-dialog-titlebar-close', function() { 
    handleEvent();
});

FIDDLE 小提琴

I believe this is the solution you are looking for - you will need to unbind the click event and re-bind your custom handler to it. 我相信这是您正在寻找的解决方案 - 您需要取消绑定click事件并将自定义处理程序重新绑定到它。

Although this thread is old, I'd still add my solution here with a hope to benefit others who search for it. 虽然这个帖子已经老了,但我仍然在这里添加我的解决方案,希望能够让其他搜索它的人受益。

var fnCustomerHandler = function() {
    alert("Here is your custom handler...");  
};

$( "#dialog" ).dialog({
        open: function(event, ui) {
            var el = $(this).closest('.ui-dialog').find('.ui-dialog-titlebar-close');
            el.off();
            el.on("click", fnCustomerHandler);
        }
    }
);

Fiddle link: 小提琴链接:

http://jsfiddle.net/morefool/n82649d5/ http://jsfiddle.net/morefool/n82649d5/

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

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