我有DIV,我根据页面上的不同选择将数据传入。我将这些值附加到数组中以创建MYSQL查询。当页面加载时,一些MySQL子句被设置为SELECT FROM JOIN ON LIMIT 1000
,并且可以在这些预加载的子句之间附加更多值以实现类似:
SELECT *
FROM abop_stats
JOIN call_outcome ON abop_stats.outcome_idfk = call_outcome.id
ORDER BY outcome_idfk ASC
LIMIT 1000
在页面加载之前,有两组JOIN
& ON
子句所以我试着编写一个if语句来检查查询的长度,如果超过一定数量,则添加第二组子句。
这没有用,所以我尝试console.log
我的数组长度,并且在加载时会打印固定值16
,并且只要将选项添加到DIV's
中,就会输出相同的值因此显示的查询(html)与console
上的查询(html)不同(因为第二组MySQL子句没有被添加)。
我的代码:
function query() {
var qry = [];
qry.push($('#select').html());
qry.push($('#selection').html());
qry.push($('#from').html());
qry.push($('#table').html());
qry.push($('#join').html());
qry.push($('#table2').html());
qry.push($('#on').html());
qry.push($('#tablejoin').html());
if (qry.length > 10) {
qry.push($('#join2').html());
qry.push($('#table3').html());
qry.push($('#on2').html());
qry.push($('#tablejoin2').html());
}
qry.push($('#precondition').html());
qry.push($('#column').html());
qry.push($('#condition').html());
qry.push($('#input').html());
qry.push($('#precondition2').html());
qry.push($('#column2').html());
qry.push($('#condition2').html());
qry.push($('#limiter').html());
var fullqry = qry.join(" ");
var length = qry.length;
console.log(qry.length);
console.log(fullqry);
return fullqry;
}
为什么我的array.length
会给出静态值?
答案 0 :(得分:2)
让我们按照代码执行来做一些简单的数学运算。
function query() {
var qry = [];// qry.length = 0;
qry.push($('#select').html()); // qry.length = 1;
qry.push($('#selection').html());// qry.length = 2;
qry.push($('#from').html());// qry.length = 3;
qry.push($('#table').html());// qry.length = 4;
qry.push($('#join').html());// qry.length = 5;
qry.push($('#table2').html());// qry.length = 6;
qry.push($('#on').html());// qry.length = 7;
qry.push($('#tablejoin').html());// qry.length = 8;
if (qry.length > 10) {// qry.length = 8; 8 > 10 = false; do not parse
qry.push($('#join2').html());
qry.push($('#table3').html());
qry.push($('#on2').html());
qry.push($('#tablejoin2').html());
}
qry.push($('#precondition').html());// qry.length = 9;
qry.push($('#column').html());// qry.length = 10;
qry.push($('#condition').html());// qry.length = 11;
qry.push($('#input').html());// qry.length = 12;
qry.push($('#precondition2').html());// qry.length = 13;
qry.push($('#column2').html());// qry.length = 14;
qry.push($('#condition2').html());// qry.length = 15;
qry.push($('#limiter').html());// qry.length = 16;
var fullqry = qry.join(" ");
var length = qry.length;// qry.length = 16; set length to 16
console.log(qry.length);// qry.length = 16; print 16
console.log(fullqry);
return fullqry;
}
你永远不会成功,因为你正在测试无法获得的价值。 更改if语句,因为此时值始终为8。对数组长度的测试在那里是没有意义的,因为除非事先有一个可能导致长度变化的循环,否则它总是相同的值。
但在此设置中,它始终为8。
在sidenode上,你可能最好使用$(“#selector”)。text()而不是.html()。这样你就不会用脚本插入流氓插件。