douzhi6365
2018-02-14 19:15
浏览 96
已采纳

确定ajax调用已完成以运行另一个ajax调用

In my project I have some select option group which load ajax data depending on previous value. Now I am having problem when I am trying to copy them to another select option group.

here is the scenario

parmanet address              Present Address
Division                      Division    
District                      District
Upzilla                       Upzilla
Union                         Union  

All of them are select field and after select each field next select option loaded by ajax. I put a checkbox and when user click the checkbox, parmanent address data should copy to present address with all the ajax call.

Now The problem is, the jquery "val" function not working because it runs before the data loaded from ajax. If I put delay to 100 ms, it working, but It's not a proper way. Is there any better way to solve this problem??

This is my code when i change division to load ajax data to division, and other option is same as like this.

$('#divisions').change(function() {
    $("#villtable").hide();
    $("#villaddform").hide();
    $.ajax({
      type: 'post',
      url: 'GetDistricts',
      data: {
        get_option:this.value
      },
      success: function(response) {
        document.getElementById("districts").innerHTML=response;
      }
    });
  });

And this is what i tried to copy data to present address group...

$.when( $('.division-prese').val(divi).trigger('change').delay( 100 ) ).then(function() {
    $.when( $('.district-prese').val(dist).trigger('change').delay( 100 ) ).then(function() {
        $.when( $('.upazilla-prese').val(upaz).trigger('change').delay( 100 ) ).then(function() {
                        $('.union-prese').val(unio).trigger('change');
        });   
    });
});

i also tried 'done', but still not working.

图片转代码服务由CSDN问答提供 功能建议

在我的项目中,我有一些select选项组,它根据以前的值加载ajax数据。 现在,当我尝试将它们复制到另一个选择选项组时,我遇到了问题。

这里是场景

  parmanet address Present 地址
Division Division 
District District 
Upzilla Upzilla 
Union Union 
   
 
 

所有这些都是select字段,然后选择每个字段后选择ajax加载的选项。 我放了一个复选框,当用户点击复选框时,parmanent地址数据应复制到所有ajax调用的当前地址。

现在的问题是,jquery“val”函数无法正常工作,因为它在从ajax加载的数据之前运行。 如果我将延迟时间设置为100毫秒,那么它可以工作,但这不是一个正确的方法。 有没有更好的方法来解决这个问题??

当我改变除法以将ajax数据加载到除法时,这是我的代码,其他选项与此类似。

  $('#divisions')。change(function(){
 $(“#villtable”)。hide(); 
 $(“#villaddform”)。hide(  ); 
 $ .ajax({
 type:'post',
 url:'GetDistricts',
 data:{
 get_option:this.value 
},
 success:function(response){  
 document.getElementById(“districts”)。innerHTML = response; 
} 
}); 
}); 
   
 
 

这就是我的意思 试图将数据复制到当前地址组...

  $。when($('。division-prese')。val(divi).trigger('change')  .delay(100))。then(function(){
 $ .when($('。district-prese')。val(dist).trigger('change')。delay(100))。then(function)  (){
 $ .when($('。upazilla-prese')。val(upaz).trigger('change')。delay(100))。then(function(){
 $('。union  -prese')。val(unio).trigger('change'); 
})  ;  
}); 
}); 
   
 
 

我也试过'完成',但仍然无法正常工作。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题