成功的AJAX功能完成前没有发生 [英] success function not occurring before AJAX complete

查看:138
本文介绍了成功的AJAX功能完成前没有发生的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jQuery的对话框 beforeClose 功能。我检查,看看用户表单数据格式正确,如果没有,返回 beforeClose ,所以用户可以重新输入信息。

I am trying to use the jQuery dialog box beforeClose feature. I am checking to see if user form data is formatted properly and if not, returning false to the beforeClose, so the user can re-enter information.

该对话框code调用函数 submituserupdateform beforeClose

The dialog box code calls the function submituserupdateform in the beforeClose:

.dialog({beforeClose: function () {
  formresult=submituserupdateform($('#myaccountdialog').find('form'));
  if (formresult !="okay"){
    return false;
 }
}})

submituserupdateform 功能检查,看看是否有在code的错误:

The submituserupdateform function checks to see if there was an error in the code:

function submituserupdateform($tgt){
  var url='./includes/admin/user/myaccount.php';
  $.ajax({
      type: "POST",
      url: url,
      data: $tgt.serialize(),
      success: function(data){
        $("#myaccountdialog").html(data);
        if ($('.error').length){
          var myresult= "notokay";
        } else {
          var myresult ="okay";
        }
      },
      dataType: "html"
    });
   return myresult;
}

我已搜查,并从Ajax调用发现返回值?,但我我已经设定 myresult 我成功的回调中。我也曾尝试 ajaxComplete 的ajaxSuccess .done 。据控制台,什么我都试过套 myresult ;我总是以结束:

I have searched, and found Return value from ajax call?, but I am already setting myresult inside my success callback. I have also tried ajaxComplete, ajaxSuccess, .done. According to the console, nothing I have tried sets myresult; I always end up with an:

未捕获的Ref​​erenceError:MyResult是一个判定没有定义

这必须是一个简单的错误,请帮助我,如果你看到我的错!

This must be a simple error, please help me if you see my mistake!

推荐答案

AJAX的异步行为将使阿贾克斯成功callback-之前,请函数返回(那是你不想要的东西的)

The asynchronous behaviour of ajax will make your function return before the ajax success callback- (that is what you don't want)

您可以添加异步

 var myresult ="":
 $.ajax({
      type: "POST",
      url: url,
      data: $tgt.serialize(),
      async:false,
      success: function(data){

这篇关于成功的AJAX功能完成前没有发生的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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