我假设任何代码填充select
设置为运行后收到响应。因此,在代码之后放置您的值选择应该可以解决问题。
(换句话说,它会去内的populateOptions()
功能。)
除此之外,这是很难提供一个解决方案,没有看到你的代码。
编辑:下面是如何这可能是工作的几个例子。其中任何一项都会比请求中的设置async: false
更好。
您可以将需要等待响应的代码放入的success:
回调中。
function populateOptions() {
$.ajax({
url: "some/path",
success: function(resp) {
// Place ANY code that needs to wait for the response in here.
// This way it will not run until the successful response is received.
}
});
}
或者你可以将需要等待另一个函数内部的响应,并调用它从success:
回调中的代码。
function populateOptions() {
$.ajax({
url: "some/path",
success: function(resp) {
// Call this function when the successful response is received.
successFunction(resp);
}
});
}
function successFunction(resp) {
// Place ANY code that needs to wait for the response in here.
}
或者说,如果populateOptions()
应以不同的方式重复使用,所以你需要一个不同的回调,可以从将在success:
回调内部调用另一个函数传递一个函数。
function myMethod() {
// Send a function as an argument when you call populateOptions()
populateOptions(function(resp){alert(resp);});
// Code that does NOT need to wait can still go here
}
function populateOptions(callback_fn) { // Receive the function that was sent
$.ajax({
url: "some/path",
success: function(resp) {
// Call the function that was sent
callback_fn(resp);
}
});
}
或采取上述同样的例子,你实际上可以使用在通过为的success:
回调函数。
function myMethod() {
// Send a function as an argument when you call populateOptions()
populateOptions(function(resp){alert(resp);});
// Code that does NOT need to wait can still go here
}
function populateOptions(callback_fn) { // Receive the function that was sent
$.ajax({
url: "some/path",
success: callback_fn(resp) // Now the function passed is the callback
});
}
请发布AJAX代码。 – joshperry 2010-06-29 13:34:56