jquery对话框:在对话框内获取对话框ID

时间:2015-03-31 04:48:12

标签: jquery jquery-ui jquery-ui-dialog

我有软件允许用户打开几个对话框。如果不对我正在做的事情进行很长的解释,是否可以从对话框中提取对话框的ID?例如,我可能有一个名为' dialog1'的对话框。然后另一个名为“对话框5”,如果我点击这些对话框中任意一个HTML的链接(不点击对话框按钮),我需要知道它的对话框是'对话框1&#39 ;或者' dialog5'。

目前我看到两种选择: 1#打开带有保存对话框ID的GET变量的对话框,然后让PHP在我需要的地方回显出它的ID(并在任何ajax表单中重新发送该信息,然后让该页面返回数据,如果对话框是刷新等等,等等....)

2#让每个页面都有一个带有特定id的HTML dom元素,然后在加载JavaScript时找到该元素,用随机生成的元素替换它(这样就不会与新的对话框发生冲突),然后使用随机生成的元素遍历DOM以动态提取ID ...

我尝试了两种方法,似乎方法2更好,因为它需要更少的维护,但它是一个丑陋的解决方案(如果DOM发生变化会破坏)。有没有更好的方法来完成这项工作?

1 个答案:

答案 0 :(得分:0)

将委托的click事件绑定到所有对话框id都存在的外部包装器。使用所有对话框的公共类(“common-class”)。

<div class="outer-wrapper">
<div id="dialog-1" class="common-class" title="Basic dialog">
<p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>
<div id="dialog-2" class="common-class" title="Basic dialog">
<p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
</div>
</div>

jQuery(".outer-wrapper").on("click",".common-class",function(){
   var dialogclick = jQuery(this).attr("id");
})