当函数传递时,this.dialog(“close”)不起作用

时间:2012-08-24 01:22:16

标签: jquery dialog

这是我的问题:

我有2个变量:

var func = function() {
   $(dialog).dialog("close");
}

var m = "hello"

我打电话给方法

this.xyz(func , m)

方法xyz看起来像

xyz : function(func, m) {
   //there is an OK button on which I am calling click event
   click: function() {
         func();
   }
}    

方法-1 现在在xyz内部,如果我用 $(对话框)替换 func(); .dialog(“关闭”); 完美无缺

方法-2 但是用func();按钮单击不会关闭对话框

我想使用方法2,但无法使其正常工作

日Thnx

2 个答案:

答案 0 :(得分:0)

您似乎忘记了新范围将丢失this,您必须bindapply新功能。

xyz : function(func, m) {
   //there is an OK button on which I am calling click event
   click: function() {
         func.apply(this);
   }
}  

答案 1 :(得分:0)

只需传递变量就可以了

xyz : function(func, m) {
 //there is an OK button on which I am calling click event
 click: func 
}