我假設任何代碼填充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