当下拉列表值更改时,我通过Ajax调用的本地CSV进行以下调用。
然后将CSV拆分为每个逗号上的数组。然后我们循环并获取正确的值。
但是,此行拒绝评估为true。我设置了断点并发现weFoundYou等于“XXBO2”而options2 [find2] .toString()确实等于“XXBO2”但if语句不会评估。
if (weFoundYou === options2[find2].toString())
完整代码:
$('#myoptions').change(function() {
csvData2 = $.ajax(
{
type: "GET",
url: "data/stats5.csv",
dataType: "text",
success: function (result2)
{
var options2 = result2.split(",");
var selecthidden2 = document.getElementById('myoptions2');
var select2 = document.getElementById('myoptions');
var selectedText = select2.options[select2.selectedIndex].text;
var q = 0;
var find = 0;
var find2 = 180;
while (find < options.length)
{
if (options[find] == selectedText)
{
var weFoundYou = "XX" + options[find - 1];
find++;
}
else
{
find++;
}
}
while (find2 < options2.length)
{
if (weFoundYou === options2[find2].toString())
{
selecthidden2.options[find2] = new Option(options2[find2+1], find2);
find2++;
}
else
{
find2++;
}
}
}
});
令人讨厌的是,FIRST if语句评估得很好!
if (options[find] == selectedText)
CSV文件示例的内容:
XXB01, Somedropdownvalue, XXB02, Somedropdownvalue2
数组内容:
XXB01, Somedropdownvalue, XXB02, Somedropdownvalue2
答案 0 :(得分:0)
CSV文件中的所有逗号都有空格吗?如果是,请尝试split(', ')
(逗号后面的空格)。它可能正在尝试匹配weFoundYou
中包含空格的字符串。另请尝试weFoundYou.toString()
,以防万一。
此外,find2从180开始,所以它在开始搜索之前跳过很多数组。变量q
似乎没有做任何事情。另外,要清理循环,既然无论是否找到匹配都会增加,请尝试:
while (find < options.length) {
if (options[find] == selectedText) {
var weFoundYou = "XX" + options[find - 1];
}
find++;
}
while (find2 < options2.length) {
if (weFoundYou === options2[find2].toString()) {
selecthidden2.options[find2] = new Option(options2[find2+1], find2);
}
find2++;
}
}