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

标签 javascript jquery jquery-ui-dialog

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

这两种方法我都试过了,

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

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 来实现!!

谢谢!!!

最佳答案

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

关于javascript - 如何使用 jQuery Dialog OK 按钮调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4298087/

相关文章:

javascript - 使用 JQuery 切换 div 后重新加载 CSS

javascript - JQuery 突出显示当前菜单项

javascript - 如何防止在 jQuery Mobile 中堆叠链接对话框的 URL

javascript - 为 Javascript 计算每个方法的平均代码行数

javascript - yarn 开始不工作 |错误 `ERR_PACKAGE_PATH_NOT_EXPORTED'

javascript - 删除第二次单击事件上的类 (jQuery)

javascript - jQuery UI 对话框仅在第一次调用时有效

jquery 对话框和用户控件

javascript - 带 http 外部链接的 SSL 网站

javascript - 将变量添加到toastr js消息框中