jQuery AJAX initial data problem for dynamic dropdowns
I have a problem with initialization of dynamically filled dropdowns in jQuery.
Basically, I have function fillCityList
and it makes AJAX call to fill the cities by the passing country.
Because this is used in Edit
form, I have a default City
value in id_cityHidden
field.
Actually the code below works well.However, because fillCityList
takes long time to fill the city list, while default city is selected, the city list may not be ready.
$(document).ready( function() {
fillCityList(1);
$('#city').val($("#id_cityHidden").val());
});
I know there is a solution like "call function at complete
stage of AJAX call" but I just need it during initialization.
One solution might be setting a timeout
or delay
between fillCityList
and
$('#city').val($("#id_cityHidden").val())
however, it is n开发者_如何学Cot a good solution of course.
What is the best way to do this ?
Thanks
I would suggest you build your ajax to use the 'complete' function, then update the value.
http://api.jquery.com/ajaxComplete/
精彩评论