jQuery UI对话框-关闭事件的问题 [英] jQuery UI dialog - problem with event on close
问题描述
当我关闭jQuery UI对话框时,我正在尝试执行特定操作.这是我的代码的简化版本:
I'm trying to perform a specific action when I close a jQuery UI dialog. Here's a simplified version of my code:
$('a.open-trigger').click(function(){
var test = 'hello';
$('#dialog').dialog({
bgiframe: true,
dialogClass: 'change',
resizable: false,
draggable: false,
modal: true,
height: 334,
width: 450,
autoOpen: false,
show: 'fade'
});
$('#dialog').dialog('open');
$('a.close-trigger').click(function(){
alert(test);
$('#dialog').dialog('close');
});
});
第一次关闭对话框时,会收到带有"hello"字样的预期警报.如果我再次打开该对话框,然后关闭它,则会收到两次"hello"警报.如果我第三次打开和关闭它,则会收到三个警报,依此类推.
The first time I close the dialog, I get the expected alert with the word "hello". If I open the dialog a second time, and close it, I get the "hello" alert twice. If I open and close it a third time, I get three alerts, and so on.
为什么这些警报会自我复制?我希望警报在关闭时仅显示一次,无论我打开/关闭对话框有多少次.
Why are these alerts duplicating themselves? I would want to the alert to only show up once on close, no matter how many times I open/close the dialog.
谢谢! 西蒙
推荐答案
每次调用.click
时,您都将附加其他事件处理程序.这就是为什么它要重复的原因.
You are attaching additional event handlers every time you call .click
. That is why it is duplicating.
$('a.close-trigger').click(function(){
alert(test);
$('#dialog').dialog('close');
});
将该代码拉出到与其他事件绑定相同的级别,并且应该可以按预期工作.
Pull that code out onto the same level as the other event binding and it should work as expected.
这篇关于jQuery UI对话框-关闭事件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!