javascript 如何使用 jQuery Dialog OK 按钮调用函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4298087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 10:55:54  来源:igfitidea点击:

How to call a function with jQuery Dialog OK button?

javascriptjqueryjquery-ui-dialog

提问by IamIronMAN

I am trying to call a function from Jquery dialog box Okbutton.

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

I have tried these two methods,

这两种方法我都试过了

1

1

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

2

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();}}
            });

Both not working!

两个都不行!

How shall i do it with jquery!!

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

Thanks!!!

谢谢!!!

回答by Mike Ruhlin

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();}}
            });

回答by Nick Craver

No need to have another reference here, just reference the function directly as the one "Ok"is bound to, like this:

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

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 way you're not inside another anonymous function where thisis a different context.

这样你就不会在另一个this不同上下文的匿名函数中。

回答by SLaks

Inside the Okcallback, thisisn't what you think it is.

Ok回调里面,this不是你想的那样。

You need to save a reference to the original this.

您需要保存对原始this.

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(); } }
            });
};