无法访问select2 AJAX URL

时间:2015-06-06 22:09:36

标签: javascript jquery ajax jquery-select2

我有国家和城市select框。内容根据国家/地区ID加载到城市框。问题是AJAX URL总是只看到countryId的初始值,尽管我可以从其他函数中访问正确的值。

$(document).ready(function() {
    var countryId = 1;

    $(".country-select").select2({
        placeholder: "Select a country",
        data: countries
    });

    $(".country-select").on("select2:select", function(e) {
        countryId = e.params.data["id"];
    });

    $(".city-select").select2({
        placeholder: "Select a city",
        ajax: {
            //countryId always equals 1
            url: "https://api.vk.com/method/database.getCities?country_id=" + countryId,
            //other options
        }
    });

    $(".somediv").click(function() {
        alert(countryId); //alerts right value
    });
});

0 个答案:

没有答案
相关问题