dialog(“close”) doesn't work inside ajax:success event handler

吃可爱长大的小学妹 提交于 2019-12-13 05:04:54

问题


I am trying to using jQuery dialog to make a user sign in. I am using ajax and devise. After users sign in, the dialog windows should close. I put dialog("close") inside bind("ajax:success"), but it doesn't work and I get error:

"cannot call methods on dialog prior to initialization; attempt to call method 'close'"

Code:

$(function(){
$(" #sign_in").click(function(){
     $('<div id="box" >').dialog({
        open: function(){ 
                 var that=this;
                 $(this).load("/users/sign_in",function(){ 
                    $("#new_user").bind("ajax:success",function(evt,data,status,xhr){
                    $("div#utility").html('welcome'+data.user+' |<a href="/users/sign_out" data-method="delete" rel="nofollow">sign out</a> ') ;
                    $(that).dialog('close');
                      })
               })
             },
        title: 'Sign in '
        });
   });
}) 

Can anyone help me figure out what the problem is?
thanks


回答1:


here is a question that has answers that might help with your problem.

jQuery: Load Modal Dialog Contents via Ajax

you can also change your selector for the dialog to $('#box').dialog({ ...

and in your ajax success callback (after you make changes) you can do $('#box').dialog('close')

EDIT: this work?

$(function(){
$(" #sign_in").click(function(){
 $('#box').dialog({
    open: function(){ 
        $(this).load("/users/sign_in");
    },
    title: 'Sign in '
    });
});

$("#new_user").bind("ajax:success",function(evt,data,status,xhr){
    $("div#utility").html('welcome'+data.user+' |<a href="/users/sign_out" data-method="delete" rel="nofollow">sign out</a> ') ;
    $('#box').dialog('close');
   })
}) 


来源:https://stackoverflow.com/questions/16463128/dialogclose-doesnt-work-inside-ajaxsuccess-event-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!