2012-11-16 30 views
1

我有一個良好的工作下一個js函數:

$(function() { 
    $(".country").click(function() { 
     var countries = Array(); 
     $(".country:checked:enabled").each(function(i, element){ 
      countries[i] = $(element).attr("id"); 
     }); 
     countries_string = countries.join(",");    
     $('#scroll').scrollTop(0); 
     $.ajax({ 
      type: 'POST', 
      url: '@Url.Action("CheckBoxCountryFilter", "CheckBox")', 
      data: {countries:countries_string}, 
      success: function (result) { 
       //console.log(result); 
       $("#wineResult").html(result); 
      }    
     }); 
     if(countries.length > 0){ 
      $("#countryImage").html('<img src="/Content/Images/icons/check.png">');    
     }else{ 
      $("#countryImage").html(''); 
     } 
     $.ajax({ 
      url: '@Url.Action("ArtikelNumber", "CheckBox")', 
      type: 'POST',     
      success: function (result) { 
       $("#artikelNumber").html(result); 
      } 
     }); 
    }); 
}); 

但也有一個問題,第一個$。阿賈克斯()不執行某個時候開始,我可以做財產以後說拳頭$ .ajax()有史以來第一次執行?謝謝

+1

呼叫第二個'阿賈克斯()在第一個的回調'的success。 – jfrej

+0

這就是ajax調用的重點。您可能不知道需要多長時間才能返回,因此您的瀏覽器可以執行其他操作。在這種情況下,另一件事是另一個Ajax調用。正如答覆者所建議的那樣,如果順序很重要,請將第二個呼叫置於第一個呼叫的回叫中。 –

回答

5

您可以強制通過第二$.ajax呼叫移動到第一個

$(function() { 
    $(".country").click(function() { 
     var countries = Array(); 
     $(".country:checked:enabled").each(function(i, element){ 
      countries[i] = $(element).attr("id"); 
     }); 
     countries_string = countries.join(",");    
     $('#scroll').scrollTop(0); 
     $.ajax({ 
      type: 'POST', 
      url: '@Url.Action("CheckBoxCountryFilter", "CheckBox")', 
      data: {countries:countries_string}, 
      success: function (result) { 
       //console.log(result); 
       $("#wineResult").html(result); 
       $.ajax({ 
        url: '@Url.Action("ArtikelNumber", "CheckBox")', 
        type: 'POST',     
        success: function (result) { 
         $("#artikelNumber").html(result); 
        } 
       }); 
      }    
     }); 
     if(countries.length > 0){ 
      $("#countryImage").html('<img src="/Content/Images/icons/check.png">');    
     }else{ 
      $("#countryImage").html(''); 
     } 
    }); 
}); 
+0

非常感謝 –