jQuery函数不具有逻辑行为 [英] jquery functions not behaving logically
问题描述
怪异的功能行为.我正在使用父函数,这是相关部分:
Weird function behavior. I'm working in a parent function, here is the relevant portion:
$("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在声称已完成之后正在打印.而且这使我选择的选项混乱了,并且不允许我像我所需要的那样自动选择.这是新手吗?我想念什么吗?我包括了萤火虫的图像,因为我认为那是最简单的.
But for some reason, console.logs from populateDep are printing AFTER it claims to be finished. And it's messing with the options of my select and not allowing me to auto-select like I need to. Is this a newbie thing? Am I missing something? I'm including an image of firebug, because I thought that would be easiest.
更新的populateDep:
Updated 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已经有一个回调,所以我对正在发生的事情很困惑.
Now, it appears not to be populating the select at all. Please note that $.getJSON has a callback already so I'm pretty confused as to what is going on.
推荐答案
如我在评论中所述,您的方法存在很多问题.
There a quite a few problems in your approach as I explained in the comments.
尝试
$("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");
populateDep.call($("select#" + prefix + "1_" + num)).done(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);
return $.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');
});
}
这篇关于jQuery函数不具有逻辑行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!