jQuery ui:不能在初始化之前在对话框上调用方法;试图调用方法“关闭" [英] jquery ui: cannot call methods on dialog prior to initialization; attempted to call method 'close'

查看:106
本文介绍了jQuery ui:不能在初始化之前在对话框上调用方法;试图调用方法“关闭"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery ui对话框,我从jquery ui网站下载了它,版本是jquery-ui-1.10.2.custom.min.js,jquery是与jquery ui捆绑在一起的jquery-1.9.1.js js,但是现在我遇到一个问题:当对话框打开并单击保存"按钮时,我希望对话框关闭,这是我的代码:

I am using jquery ui dialog, I download it from jquery ui website, version is jquery-ui-1.10.2.custom.min.js, and jquery is jquery-1.9.1.js which is bundled with jquery ui js, but now I am encountering a question: when dialog is opened and click save button, I want the dialog to be closed, here is my code:

$(function(){
 $("#dialog-form").dialog({
     autoOpen: false,
     height: 350,
     width: 450,
     modal: true,
     buttons: {
       "save": function() {
           if(!checkDept()){
               return ;
           }
           $.post('dept_save.do', 
                 {'dept.deptId':$("#dialog_dept_deptId").val(), 
                  'dept.deptName':$("#dialog_dept_deptName").val(),
                  'dept.manager':$("#dialog_dept_manager").val(),
                 },function(data, status, xhr){
                     if(status == 'success'){
                         alert('save success');
                         $(this).dialog("close");
                     }else{
                         alert('error:'+data);
                     }
                  }
                 ,"json");
       }
     },
     close: function() {
         $(this).dialog("close");
     }
   });

     /* to open dialog*/
    $("#add").click(function(){
        $("#dialog-form").dialog("open");
    });

现在,当我关闭保存成功"弹出对话框时,dialog-form对话框没有关闭,并且发生了错误:

now when I close the 'save success' popuped dialog, dialog-form dialog was not closed, and an error occurs:

未捕获的错误:初始化之前无法在对话框上调用方法;尝试调用方法'close'jquery-1.9.1.js:507.

Uncaught Error: cannot call methods on dialog prior to initialization; attempted to call method 'close' jquery-1.9.1.js:507.

还有另一个错误:

未捕获到的SyntaxError:jquery-1.9.1.js:541意外令牌

Uncaught SyntaxError: Unexpected token o jquery-1.9.1.js:541

谢谢.

推荐答案

一旦进入$.post(),您将丢失this的上下文. 在$ .post之前,将上下文this保存在该 save 按钮函数内部的变量中.

You are losing the context of this once you are inside of $.post(). Before your $.post, save the context this in a variable inside of that save button function.

$('#dialog-form').dialog({
     // .....
     buttons: {
        'save': function() { 

            var $this = $(this); 
                      // -this- is still the original context 
                      // of $("#dialog-form")

            $.post({
               /// ...
               $this.dialog('close'); // <-- used here
            });
        }       
    }
});

这篇关于jQuery ui:不能在初始化之前在对话框上调用方法;试图调用方法“关闭"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆