首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用jQuery对话框中的OK按钮调用函数?

如何使用jQuery对话框中的OK按钮调用函数?
EN

Stack Overflow用户
提问于 2010-11-29 01:44:27
回答 3查看 17.5K关注 0票数 1

我正在尝试从Jquery对话框Ok按钮调用一个函数。

我试过这两种方法,

1

代码语言:javascript
复制
this.commentDialog = function(){
        $("#commentDialog").dialog( "destroy" );
        html = "<div id='cmtDialog'>";
        html += "Comment<textarea id='comment'></textarea></div>";
        $("#commentDialog").html(html);
        $("#commentDialog").dialog({
            title:"Search Result",
            bgiframe: true,
            height: 'auto',
            width: 'auto',
            modal: true,autoOpen: true,
            buttons: { "Ok": function() { this.saveComment();}}
            });

2

代码语言:javascript
复制
this.commentDialog = function(){
        $("#commentDialog").dialog( "destroy" );
        html = "<div id='cmtDialog'>";
        html += "Comment<textarea id='comment'></textarea></div>";
        $("#commentDialog").html(html);
        $("#commentDialog").dialog({
            title:"Search Result",
            bgiframe: true,
            height: 'auto',
            width: 'auto',
            modal: true,autoOpen: true,
            buttons: { "Ok": function() { saveComment();}}
            });

两者都不起作用!

我该如何用jquery做到这一点!!

谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-11-29 01:50:48

代码语言:javascript
复制
this.commentDialog = function(){
        // save a reference of "this" and use it later.
        var me = this;

        $("#commentDialog").dialog( "destroy" );
        html = "<div id='cmtDialog'>";
        html += "Comment<textarea id='comment'></textarea></div>";
        $("#commentDialog").html(html);
        $("#commentDialog").dialog({
            title:"Search Result",
            bgiframe: true,
            height: 'auto',
            width: 'auto',
            modal: true,autoOpen: true,
            buttons: { "Ok": function() { 
                 // use me instead of this, as this now refers to the function.
                 me.saveComment();}}
            });
票数 6
EN

Stack Overflow用户

发布于 2010-11-29 01:55:37

这里不需要另一个引用,只需直接引用函数,就像"Ok"绑定的函数一样,如下所示:

代码语言:javascript
复制
this.commentDialog = function(){
    $("#commentDialog").dialog( "destroy" );
    html = "<div id='cmtDialog'>";
    html += "Comment<textarea id='comment'></textarea></div>";
    $("#commentDialog").html(html);
    $("#commentDialog").dialog({
        title:"Search Result",
        bgiframe: true,
        height: 'auto',
        width: 'auto',
        modal: true,autoOpen: true,
        buttons: { "Ok": this.saveComment }
    });
};

这样,您就不会处于另一个匿名函数中,而this是一个不同的上下文。

票数 3
EN

Stack Overflow用户

发布于 2010-11-29 01:49:05

Ok回调中,this并不是您想象的那样。

您需要保存对原始this的引用。

代码语言:javascript
复制
this.commentDialog = function() {
        var html = "<div id='cmtDialog'>Comment<textarea id='comment'></textarea></div>";
        var me = this;          //Save the original this
        $("#commentDialog")
            .dialog( "destroy" )
            .html(html);
            .dialog({
                title:"Search Result",
                bgiframe: true,
                height: 'auto',
                width: 'auto',
                modal: true,autoOpen: true,
                buttons: { "Ok": function() { me.saveComment(); } }
            });
};
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4298087

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档