-1
遇到我的變量'html'有問題。我認爲我的範圍正確,但是當我運行這段代碼時會發生奇怪的事情。 'html'變量的第一個提示會產生一個空白結果,然後用相同的'html'變量填充我的選擇列表,然後我會再次提醒'html'變量並顯示選項。Javascript範圍問題可能
如果我刪除了兩個警報列表中不會彈出
function populateMakes(years)
{
var makes = new Array();
var html = "";
$.each(years, function() {
var uri = "/api/make?year=" + this;
$.getJSON(uri, function (data) {
$.each(data, function() {
if (jQuery.inArray(this.Id, makes) == -1)//makes not contain make
{
makes.push(this.Id);
html += "<option value=" + this.Id + ">" + this.Value + "</option>";
}
});
});
});
alert(html);
$("#Make").html(html);
alert(html);
$("#MakeSection").removeClass("hidden");
};
文件準備腳本
$(document).ready(function() {
populateYears();
$("#Year").change(function() {
$("#MakeSection").addClass('hidden');
$("#ModelSection").addClass('hidden');
$("#SubModelSection").addClass('hidden');
populateMakes($("#Year").val());
});
$("#Make").change(function() {
$("#ModelSection").addClass('hidden');
$("#SubModelSection").addClass('hidden');
populateModels($("#Year").val(), $("#Make").val());
});
$("#Model").change(function() {
$("#SubModelSection").addClass('hidden');
//
});
$("#Clear").click(function() {
$("#YearSection").addClass('hidden');
$("#MakeSection").addClass('hidden');
$("#ModelSection").addClass('hidden');
$("#SubModelSection").addClass('hidden');
populateYears();
})
});
很高興你自己找到答案並將其發佈給其他人。這是一個容易犯的錯誤。 – Lazarus 2015-02-11 20:36:04
我是新來的異步世界。我正在弄清楚這一點。現在我正在研究循環API調用和緩存。我的例子實際上並不是100%,即使它工作,我也是如此。我希望等到所有調用都完成之後再對數據做任何事情。 – timlint 2015-02-12 16:06:17