1
奇怪的函數行爲。我在父功能的工作,這裏是有關部分:jquery函數不能在邏輯上運行
$("select#" + prefix + "1_" + num).val(smart_cat_data['cat_lvl2_id']);
Foundation.libs.forms.refresh_custom_select($("select#" + prefix + "1_" + num), true);
console.log("initial value and change DONE");
$.when(populateDep.call($("select#" + prefix + "1_" + num)),
console.log("loading dependent select DONE"),
console.log("setting " + prefix + "2_" + num + " TO: " + smart_cat_data['cat_lvl3_id'])
).then(function(x){
$("#" + prefix + "2_" + num + " > option").each(function() {
console.log(this.text + ' ' + this.value);
});
$("select#" + prefix + "2_" + num).val(smart_cat_data['cat_lvl3_id']);
Foundation.libs.forms.refresh_custom_select($("select#" + prefix + "2_" + num), true);
console.log($("select#" + prefix + "2_" + num).val());
});
的呼叫:
function populateDep(){
console.log('populateDep');
id_in_str = $(this).attr('id');
console.log('PROCESSING:' + id_in_str);
var ctrl = id_in_str.slice(0,3); // the second param is POSITION not LENGTH!
var this_select = parseInt(id_in_str.slice(3,4), 10); // the second param is POSITION not LENGTH!
var row = id_in_str.slice(5);
$.getJSON("/cat_dropdowns",{parent_id: $(this).val(), required: '1', ajax: 'true'}, function(j){
var options = '';
for (var i = 0; i < j.length; i++) {
options += '<option value="' + j[i].value + '">' + j[i].text + '</option>';
}
var next_select = ++this_select;
$("select#" + ctrl + next_select + "_" + row).html(options);
$("#" + ctrl + next_select + "_" + row + " > option").each(function() {
console.log('populateDep: ' + this.text + ' ' + this.value);
});
Foundation.libs.forms.refresh_custom_select($('#' + ctrl + next_select + "_" + row), true);
Foundation.libs.forms.assemble();
console.log('this should be after the selects but before the end');
});
console.log('populateDep DONE');
}
但由於某些原因,從populateDep console.logs打印後,聲稱要完蛋。它與我的選擇的選項搞亂,不允許我自動選擇像我需要的。這是一個新手嗎?我錯過了什麼嗎?我包括一個螢火蟲的圖像,因爲我認爲這將是最簡單的。
更新populateDep:現在
function populateDep(){
console.log('populateDep');
id_in_str = $(this).attr('id');
console.log('PROCESSING:' + id_in_str);
var ctrl = id_in_str.slice(0,3); // the second param is POSITION not LENGTH!
var this_select = parseInt(id_in_str.slice(3,4), 10); // the second param is POSITION not LENGTH!
var row = id_in_str.slice(5);
$.when($.getJSON("/cat_dropdowns",{parent_id: $(this).val(), required: '1', ajax: 'true'}, function(j){
var options = '';
for (var i = 0; i < j.length; i++) {
options += '<option value="' + j[i].value + '">' + j[i].text + '</option>';
}
})).then(function(options){
console.log('starting then');
var next_select = ++this_select;
$("select#" + ctrl + next_select + "_" + row).html(options);
$("#" + ctrl + next_select + "_" + row + " > option").each(function() {
console.log('populateDep: ' + this.text + ' ' + this.value);
});
Foundation.libs.forms.refresh_custom_select($('#' + ctrl + next_select + "_" + row), true);
Foundation.libs.forms.assemble();
console.log('this should be after the selects but before the end');
});
console.log('populateDep DONE');
}
,它似乎不被填充選擇可言。請注意,$ .getJSON已經有一個回調,所以我很困惑到底發生了什麼。
你需要傳遞承諾對象'$ .when'你不這樣做,可能對問題 –
同樣的原因,'console.log'應該異步調用之前執行回功能。基本上,在任務'$ .getJSON'完成後,回調被觸發,但是是異步的,'$ .getJSON'可以讓其他代碼在關閉時執行並執行其操作。 –
@JasonNichols對不起,我不明白。我希望在$ .getJSON完成後發生在populateDep中的$ .getJSON之後發生的所有事情。我應該在什麼時候包裝它? –