jQuery .load() 具有成功功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4734648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:50:37  来源:igfitidea点击:

.load() with success function

jqueryajax

提问by Alfie

I know this has been asked many times before...

我知道这已经被问过很多次了......

I have a simple ('#Div').load(file.php?id='+id+'&page='+page)

我有一个简单的 ('#Div').load(file.php?id='+id+'&page='+page)

The on click event acts as follows

点击事件的作用如下

function recp(id) {  
  var user = $('#page').attr('page'); 
  $("#button").hide()
  $("#loading").html('<img src="images/loader.gif">');
  $('#div').load('file.php?id='+id+'&page='+page); 
  $("#loading").empty();
  $("#button1").show()
}

I have a similar process on my site with ajax post, works very nicely, but I can't seem to get this to work with .load().

我在我的网站上有一个类似的过程,使用 ajax post,效果很好,但我似乎无法让它与.load().

Would need a bit of advice. Cheers.

需要一点建议。干杯。

回答by lonesomeday

The problem is that loadis asynchronous. The function call returns beforethe AJAX request is complete. If you have code that needs to be executed after the request is complete, you need to use the completecallback specified in the API:

问题是它load是异步的。函数调用AJAX 请求完成之前返回。如果有请求完成后需要执行的代码,则需要使用API中complete指定的回调:

$('#div').load('file.php?id='+id+'&page='+page, function(){
    $("#loading").empty();
    $("#button1").show();
});