jquery如何获取打开对话框的按钮 [英] jquery how to get the button that has opened the dialog
问题描述
$('#dialog')。对话框({
autoOpen:false,
按钮:{
Ok:function(){
$(this).dialog(close);
},
取消:function(){
$(this).dialog(close);
}
},
打开:function(event,ui){
// HERE :::如何获取HTML对象打开对话框的元素
}
});
这被调用:
$('a')。live('click',function(){
$('#dialog')。对话框('open');
}) ;
我想知道哪个< a>
标签已调用该对话框。这是可能吗?
谢谢!
在你的 .live()
处理程序可以存储引用点击的元素使用 .data()
,像这样:
$('a')。live('click',function(){
$('#dialog')。data('opener',this).dialog('open');
});
然后,稍后再来,您可以从 $('#dialog')。data('opener')
或 $。data(this,'opener')
打开
回调(因为 this
指的是对话框元素)的情况。例如,您的打开
函数可能如下所示:
open:function (event,ui){
$(this).html(我被打开了+ $ .data(this,'opener')。
}
这将显示 id
您点击的锚点的属性打开对话框...你可以做任何你想要的, $。data(this,'opener')
是指< a />
DOM元素。
I have a dialog that is opened by many buttons. How can I know which button has opened that dialog?
$('#dialog').dialog({
autoOpen: false,
buttons: {
"Ok": function() {
$(this).dialog("close");
},
"Cancel": function() {
$(this).dialog("close");
}
},
open: function(event, ui) {
//HERE ::: how to get an HTML OBJECT TO THE ELEMENT THAT OPENED THE DIALOG
}
});
This is called by:
$('a').live('click',function(){
$('#dialog').dialog('open');
});
I want to know which <a>
tag has called that dialog. Is this possible?
Thanks!
In your .live()
handler you could store a reference to the element that was clicked on using .data()
, like this:
$('a').live('click',function(){
$('#dialog').data('opener', this).dialog('open');
});
Then to get it later you can grab it from $('#dialog').data('opener')
, or $.data(this, 'opener')
in the case of the open
callback (because this
refers to the dialog element). For example your open
function may look like this:
open: function(event, ui) {
$(this).html("Hi, I was opened by: " + $.data(this, 'opener').id);
}
This would show the id
property of the anchor you clicked on to open the dialog...you can do whatever you want really, $.data(this, 'opener')
refers to the <a />
DOM element.
You can give try a demo of this here
这篇关于jquery如何获取打开对话框的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!