這是一個工作良好的JavaScript代碼的一部分。 但我想,以顯示//練習2行中的變量選項:javascript - 這段代碼有什麼錯誤?
if(profId==10){
//alert(profId);
$("#div_sel_residentType").show("slow");
var selectElm="<label for=\"sel_residentType\">Sélectionniez le Type du Résident:</label><select class=\"form-control\" id=\"sel_residentType\"><option value=\"0\" selected=\"\">Type Résident</option>";
var options ="";
$.get("../api/v1/get/menus/typeresident.json.php", function(dataset, status){
for (var index in dataset){
options = options + "<option value=\""+dataset[index].id+"\">"+dataset[index].description+"</option>";
//console.log(options);
}
console.log(options);//Ex1
});
console.log(options);//Ex2
selectElm = selectElm + options + "</select>";
//console.log(selectElm);
//$("#div_sel_residentType").html(selectElm);
}
我想了解爲什麼它顯示console.log(options);//Ex1
但不console.log(options);//Ex2
執行'$ .get'調用後立即執行'Ex2'。呼叫收到響應後執行'Ex1'。 –
'$ .get'是異步的,這意味着當你的響應可用時,你交給它的函數將被調用。與此同時,你的代碼已經被執行,來到// Ex2,其中選項仍然是「」。 – connexo