使用 .each 访问 jquery 中的 JSON 数组对象
Access JSON array objects in jquery using .each
我有一系列 JSON 数组,每个数组中都有对象,其中一个数组是在 .change 函数中选择的,数组在 if 语句中。
var country_data;
$("#job").change(function () {
selectedValue = $("#job option:selected").val();
if (selectedValue == "job_1") {
country_data = [{
"country_id": 1,
"country": "Luxembourg",
"local_wage": "407",
"wage": "489",
"exchange": "0.98"
}, {
"country_id": 2,
"country": "Norway",
"local_wage": "3200",
"wage": "378",
"exchange": "9.57"
}, {
"country_id": 3,
"country": "Austria",
"local_wage": "290",
"wage": "337",
"exchange": "0.87"
}];
} else if (selectedValue == "job_2") {
country_data = [{
"country_id": 1,
"country": "Luxembourg",
"local_wage": "874",
"wage": "654",
"exchange": "0.24"
}, {
"country_id": 2,
"country": "Norway",
"local_wage": "741",
"wage": "365",
"exchange": "4.77"
}, {
"country_id": 3,
"country": "Austria",
"local_wage": "854",
"wage": "634",
"exchange": "0.43"
}];
} else if (selectedValue == "job_3") {
country_data = [{
"country_id": 1,
"country": "Luxembourg",
"local_wage": "854",
"wage": "985",
"exchange": "0.25"
}, {
"country_id": 2,
"country": "Norway",
"local_wage": "645",
"wage": "874",
"exchange": "5.55"
}, {
"country_id": 3,
"country": "Austria",
"local_wage": "201",
"wage": "256",
"exchange": "0.78"
}];
}
}).change();
我还有一个使用 .each 访问所选数组数据的函数,但我似乎无法让它访问所选 JSON 数组中的数据。
//variable country_data below wont access the selected array
$.each(country_data, function(i) {
var regEx = new RegExp('^' + searchStr + '\w*\b','i');
if (country_data[i].country.match(regEx) ||
(i == 3 && searchStr.toLowerCase() == "us") ||
(i == 4 && ((searchStr.toLowerCase() == "uk") ||
("Great Britain".match(regEx)) ||
("Britain".match(regEx)) ||
("England".match(regEx)) ||
("Wales".match(regEx)) ||
("Scotland".match(regEx)) ||
("Northern Ireland".match(regEx))
)
)
)
$suggestionList.append('<li class="country_result" role="option"><a href="#'+country_data[i].country_id+'">' + country_data[i].country + '</a></li>');
if (country_data[i].country.toLowerCase() === searchStr.toLowerCase()) {
exactMatch = true;
select_current_country(i+1);
return;
}
});
我收到错误“无法读取未定义的 属性 'length'”。如何修改 .each 函数以便我可以访问对象数据?非常感谢任何帮助!谢谢
因为您的变量 country_data 在脚本第一次运行时将是未定义的。
不要使用 ifelse - 使用 switch。
我有一系列 JSON 数组,每个数组中都有对象,其中一个数组是在 .change 函数中选择的,数组在 if 语句中。
var country_data;
$("#job").change(function () {
selectedValue = $("#job option:selected").val();
if (selectedValue == "job_1") {
country_data = [{
"country_id": 1,
"country": "Luxembourg",
"local_wage": "407",
"wage": "489",
"exchange": "0.98"
}, {
"country_id": 2,
"country": "Norway",
"local_wage": "3200",
"wage": "378",
"exchange": "9.57"
}, {
"country_id": 3,
"country": "Austria",
"local_wage": "290",
"wage": "337",
"exchange": "0.87"
}];
} else if (selectedValue == "job_2") {
country_data = [{
"country_id": 1,
"country": "Luxembourg",
"local_wage": "874",
"wage": "654",
"exchange": "0.24"
}, {
"country_id": 2,
"country": "Norway",
"local_wage": "741",
"wage": "365",
"exchange": "4.77"
}, {
"country_id": 3,
"country": "Austria",
"local_wage": "854",
"wage": "634",
"exchange": "0.43"
}];
} else if (selectedValue == "job_3") {
country_data = [{
"country_id": 1,
"country": "Luxembourg",
"local_wage": "854",
"wage": "985",
"exchange": "0.25"
}, {
"country_id": 2,
"country": "Norway",
"local_wage": "645",
"wage": "874",
"exchange": "5.55"
}, {
"country_id": 3,
"country": "Austria",
"local_wage": "201",
"wage": "256",
"exchange": "0.78"
}];
}
}).change();
我还有一个使用 .each 访问所选数组数据的函数,但我似乎无法让它访问所选 JSON 数组中的数据。
//variable country_data below wont access the selected array
$.each(country_data, function(i) {
var regEx = new RegExp('^' + searchStr + '\w*\b','i');
if (country_data[i].country.match(regEx) ||
(i == 3 && searchStr.toLowerCase() == "us") ||
(i == 4 && ((searchStr.toLowerCase() == "uk") ||
("Great Britain".match(regEx)) ||
("Britain".match(regEx)) ||
("England".match(regEx)) ||
("Wales".match(regEx)) ||
("Scotland".match(regEx)) ||
("Northern Ireland".match(regEx))
)
)
)
$suggestionList.append('<li class="country_result" role="option"><a href="#'+country_data[i].country_id+'">' + country_data[i].country + '</a></li>');
if (country_data[i].country.toLowerCase() === searchStr.toLowerCase()) {
exactMatch = true;
select_current_country(i+1);
return;
}
});
我收到错误“无法读取未定义的 属性 'length'”。如何修改 .each 函数以便我可以访问对象数据?非常感谢任何帮助!谢谢
因为您的变量 country_data 在脚本第一次运行时将是未定义的。 不要使用 ifelse - 使用 switch。