2

我有下一个运行良好的 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() 没有先执行,我可以做一些第一个 $.ajax() 曾经先执行的事情吗?谢谢你

4

1 回答 1

6

您可以通过将第二个$.ajax呼叫移动到success第一个呼叫来强制执行此操作

$(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('');
        }
    });
});
于 2012-11-16T13:54:41.273 回答