我感觉这是一个简单的修复,但我一生都无法找到错误。当我运行此脚本时,我不断收到未定义的值,该脚本正在检查 JSON 文件中的数组中的 bool 数据。我把这一切都放进了 fiddle 里。滚动到 JS 部分的底部。这是为那些不想切换选项卡的人提供的代码。
$.ajax({
url: "/echo/json/",
data: data,
type: "POST",
success: function (response) {
//Loop Start
for (var fund in response.fundClass){
console.log(fund.class);
if(fund.xbrl == false){
$(fund.class + " .xbrl").addClass(".hide");
}
if(fund.prospSum == false){
$(fund.class + " .prospSum").addClass(".hide");
}
}
//Loop End
}
});
最佳答案
问题在于您将“response.fundClass”视为对象,而它是一个数组。
for (var i = 0; i < response.fundClass.length; i++) { // looping the Array
var fund = response.fundClass[i]; // get the object from the Array
console.log(fund.class);
if (fund.xbrl == false) {
$(fund.class + " .xbrl").addClass(".hide");
}
if (fund.prospSum == false) {
$(fund.class + " .prospSum").addClass(".hide");
}
}
关于javascript - 将 JSON 传递到循环中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22847760/