jquery如何获取打开对话框的按钮 [英] jquery how to get the button that has opened the dialog

查看:143
本文介绍了jquery如何获取打开对话框的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由许多按钮打开的对话框。如何知道哪个按钮已打开该对话框?

  $('#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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆