动态下拉列表的jQuery AJAX初始数据问题

时间:2022-06-01 17:56:01

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.

我在jQuery中初始化动态填充的下拉列表时遇到问题。基本上,我有函数fillCityList,它通过AJAX调用来填充过往国家的城市。因为这是在编辑表单中使用,我在id_cityHidden字段中有一个默认的City值。实际上下面的代码运行良好。但是,因为fillCityList需要很长时间来填充城市列表,所以选择默认城市时,城市列表可能没有准备好。

$(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 not a good solution of course.

我知道有一个解决方案,比如“在AJAX调用的完整阶段调用函数”,但我只是在初始化时需要它。一种解决方案可能是在fillCityList和$('#city')之间设置超时或延迟.val($(“#id_cityHidden”)。val())但是,它当然不是一个好的解决方案。

What is the best way to do this ?

做这个的最好方式是什么 ?

Thanks

谢谢

1 个解决方案

#1


1  

I would suggest you build your ajax to use the 'complete' function, then update the value.

我建议你构建你的ajax来使用'complete'函数,然后更新值。

http://api.jquery.com/ajaxComplete/

http://api.jquery.com/ajaxComplete/

#1


1  

I would suggest you build your ajax to use the 'complete' function, then update the value.

我建议你构建你的ajax来使用'complete'函数,然后更新值。

http://api.jquery.com/ajaxComplete/

http://api.jquery.com/ajaxComplete/