我在我的html中有一個選擇,並希望在頁面加載時通過ajax添加選項。選項值在我的數據庫中,我通過調用ajax獲取它們。爲此,我正在使用JavaScript編寫一個類,但運行時無法獲取數據。請看看:在jquery中訪問類變量
--- Main.js ---
function MyLoader() {
this._clients = null;
this._code = null;
}
Loader.prototype = {
var context = this;
loadClients: function() {
$.ajax({
url: "my/php/",
type: "POST",
data: {...},
success: function(response) {
context._clients = response;
}
});
},
getCode: function() {...}
};
然後,我有以下幾點:
$(document).ready(function() {
var loader = new Loader();
loader.loadClients();
alert(loader._clients);
//Here I want to add my options to the select
});
我的警報始終返回null,我不明白爲什麼。我需要將我的數據保存在課程中,以便在需要時隨時訪問它們。
你能指點我的方向嗎?謝謝您的回答。
謝謝!它工作正常:) – user1671731