これは正常に動作しているJavaScriptコードの一部です。 しかし、私は// Ex2のラインで変数のオプションを表示したい: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
'Ex2'は' $ .get'呼び出しの直後に実行されます。 'Ex1'は、呼び出しが応答を受け取った後に実行されます。 –
'$ .get'は非同期です。つまり、応答が利用可能になると、それに渡す関数が呼び出されます。その間、あなたのコードはすでに実行され、// Ex2に来ますが、optionsはまだ ""です。 – connexo