我有国家和城市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
});
});