问题
I've seen lots of questions and solutions to problems like this but nothing has worked for me. I have this:
function() {
$("#bdiv").load("bosses.php #icc10n",function(){
return $("#bdiv").html();
});
}
But it's not working. To clarify, I want to load content into #bdiv and then return the contents of #bdiv. But it seems that $("#bdiv").html() is being returned before the content is loaded even though I've put it in a callback function.
回答1:
$("#bdiv").load("bosses.php #icc10n",function(data){
// use the data param
// e.g. $(data).find('#icc10n')
});
回答2:
as far as I know you cannot make a return statement in the callback function of a $.ajax(), $.post(), $.get(), etc.. method. You could, however, store the 'data' value in a variable declared outside the function, and then set the value of that variable when the callback function executes. And there is a variety of other options.
回答3:
You can't do that.
AJAX is asynchronous, meaning that your function will return before the server sends a response.
You need to pass the value to your caller using a callback, the way $.load does.
来源:https://stackoverflow.com/questions/3973933/jquery-load-callback-function