gpt4 book ai didi

javascript - 等待 Ajax 完成执行其他功能

转载 作者:行者123 更新时间:2023-11-28 12:28:29 34 4
gpt4 key购买 nike

我正在使用 Ajax 更新页面中的一些值。然后,完成后我需要使用该值执行其他功能。

我将一个函数放在另一个函数之后,但即使这样,第二个函数也不会等待 Ajax 完成。

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

最佳答案

使用 jQuery Ajax 实际上非常简单。

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

查看 jQuery Ajax 文档:http://api.jquery.com/jquery.ajax/

编辑:既然您使用 GET 作为请求类型,为什么不使用 jQuery.get?在这里,您可以使用此代码。简单干净。

此外,如果它适合您,请不要忘记将其标记为答案。我们不希望 StackOverflow 出现无答案的问题,不是吗?

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

关于javascript - 等待 Ajax 完成执行其他功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25129167/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com