等待阿贾克斯完成做其他功能 [英] Wait Ajax finish to do other function

查看:146
本文介绍了等待阿贾克斯完成做其他功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网页使用Ajax更新一些值。然后,之后就是完成我需要做其他的功能,使用该值。

我把一个函数后,其他的,但即使是在这样的第二个功能是不是等待Ajax来完成。

  $(文件)。就绪(函数(){
    $(数据)。模糊(函数(){
        VAR ID = $(本).attr('身份证');
        VAR值= $(本)。html的();
        变种AD = id.split(';');

        更新(勇武,ID);
        功能2(AD [1]);


    });
});


功能更新(值,ID){
    如果(window.XMLHtt prequest){
           // $ C $下IE7 +,火狐,Chrome,歌剧,Safari浏览器
           XMLHTTP =新XMLHtt prequest();
         }其他{// code对IE6,IE5
           XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
         }
         xmlhttp.onreadystatechange =功能(){
           如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200){
             的document.getElementById(div_table)的innerHTML = xmlhttp.responseText。
           }
         }
        xmlhttp.open(GET,?update.php值=+价值+和ID =+ ID,真实);
        xmlhttp.send();

}


函数功能2(广告){
    。VAR NAME_1 = $(#NAME_1)HTML();跨度> //这部分是内部&LT更新;通过Ajax的功能
    $('#'+广告)。html的(NAME_1);
}
 

解决方案

它实际上是用jQuery的Ajax真的很简单。

  $。阿贾克斯({
    网址:数据/检索,
    成功:函数(结果){
       //调用你的函数
       功能2(结果);
 }});
 

看一看jQuery的阿贾克斯文档在这里: http://api.jquery.com/jquery。 AJAX /

修改:由于您使用GET作为请求类型,为什么不使用jQuery.get?在这里,你可以使用这个code。简单干净。

另外,不要忘了,如果你的作品,以纪念这个作为回答。我们不希望回答少的问题在这里StackOverflow的,是吗?

  $(文件)。就绪(函数(){
    $(数据)。模糊(函数(){
        VAR ID = $(本).attr('身份证');
        VAR值= $(本)。html的();
        变种AD = id.split(';');

        更新(值,ID);
    });
});


功能更新(值,ID){
    $获得(update.php,{值:值,标识:标识},功能(数据){
         //调用你的函数
         函数2(数据);
    });
}


函数功能2(广告){
    。VAR NAME_1 = $(#NAME_1)HTML();跨度> //这部分是内部&LT更新;通过Ajax的功能
    $('#'+广告)。html的(NAME_1);
}
 

I am updating some values in my page using Ajax. And then, after is finishes I need to do other function, using that values.

I am putting one function after the other, but even in that way the second function is not waiting the Ajax to finish.

$(document).ready(function(){
    $(".data").blur(function(){
        var id = $(this).attr('id');
        var value = $(this).html();
        var ad = id.split(';');

        Update(valor, id);
        Function2(ad[1]);


    });
});


function Update(value, id){
    if (window.XMLHttpRequest) {
           // code for IE7+, Firefox, Chrome, Opera, Safari
           xmlhttp=new XMLHttpRequest();
         } else { // code for IE6, IE5
           xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
         }
         xmlhttp.onreadystatechange=function() {
           if (xmlhttp.readyState==4 && xmlhttp.status==200) {
             document.getElementById("div_table").innerHTML=xmlhttp.responseText;
           }
         }
        xmlhttp.open("GET","update.php?value="+value+"&id="+id,true);
        xmlhttp.send();

}


function Function2(ad){
    var name_1 = $("#name_1").html(); //This part is updated inside a <span> by the Ajax function
    $('#'+ad).html(name_1);  
}

解决方案

It's actually really simple with jQuery Ajax.

$.ajax({
    url:"data/retrieve",
    success:function(result){
       //call your function
       Function2(result);
 }});

Have a look at jQuery Ajax documentation here: http://api.jquery.com/jquery.ajax/

Edit: Since you're using GET as your request type, why not use jQuery.get? Here, you can use this code. Simple and clean.

Also, don't forget to mark this as the answer if it works for you. We don't want answer-less questions here to StackOverflow, do we?

$(document).ready(function(){
    $(".data").blur(function(){
        var id = $(this).attr('id');
        var value = $(this).html();
        var ad = id.split(';');

        Update(value, id); 
    });
});


function Update(value, id){
    $.get("update.php", {value: value, id: id}, function (data) {
         //call your function
         Function2(data);
    });
}


function Function2(ad){
    var name_1 = $("#name_1").html(); //This part is updated inside a <span> by the Ajax function
    $('#'+ad).html(name_1);  
}

这篇关于等待阿贾克斯完成做其他功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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