【发布时间】:2021-02-17 23:27:28
【问题描述】:
你们能帮我解决这个问题吗?我正在尝试编写一个带有 2 级链选择的脚本,但我遇到了这样的错误:
scripts.js:76 Uncaught TypeError: Cannot read property 'forEach' of 未定义
你有什么想法吗?错误显示在 console.log 最后 fucntion 和看起来像这样
未捕获的类型错误:无法读取未定义的属性“forEach” 在 cmo (scripts.js:76) 在 setTown (scripts.js:66) at scripts.js:61 cmo@scripts.js:76 setTown@scripts.js:66(匿名)@scripts.js:61
这是我的代码
var json = {
"region":{
"Warmińsko - mazurskie":{
"town":{
"Olsztyn":["Mercury", "Novotel","Sheraton", "Radisson", "Gołębiewski"],
"Elbląg": ["Mercury", "Novotel","Sheraton", "Radisson Blue", "Gołębiewski"],
"Iława":["Mer", "Novotel","Sheraton", "Radisson", "Gołębiewski"],
"Ostróda":["Mercury", "Novotel","Mariot", "Radisson", "Gołębiewski"],
"Giżycko":["Mercury", "Novotel","Sheraton", "Vienna House", "Gołębiewski"],
}
},
"Małopolskie":{
"town":{
"Kraków":["Kossak", "Novotel","Sheraton", "Radisson", "Stary"],
"Tarnów":["Mercury", "Novotel","Sheraton", "Tarnovia", "Ibis"],
"Oświęcim":["Mercure", "Golden Tulip","Sheraton", "Radisson", "Hampton"],
"Skała":["Focus", "Novotel","Sheraton", "Radisson", "Zamek"],
"Wieliczka":["Mercurius", "Novotel","Sheraton", "Arche", "Blue Star"],
}
},
"Podlaskie":{
"town":{
"Białystok":["Altus", "Deo","Sheraton", "Radisson Blu", "Aquarion"],
"Suwałki":["Merr", "Novotel","Ibis", "Radisson Red", "Arche"],
"Łomża":["Mercury 2", "Telios","Sheraton", "Blue", "DeSilva"],
"Augustów":["Mariot", "Unicus","Hampton", "Ibis Budget", "Ibis Styles"],
"Zambrów":["Golden", "Blue Star","Sheraton", "Osteria", "Rafles"],
}
},
"Podkarpackie":{
"town":{
"Rzeszów":["Blue Star", "Notel","Radius", "Puławski", "Grębiewski"],
"Jasło":["Mercury2", "Novotel2","Sheraton2", "Radisson2", "Gołębiewski2"],
"Krosno":["Mercury3", "Novotel3","Sheraton3", "Radisson3", "Gołębiewski3"],
"Ustrzyki Górne":["Mercury4", "Novotel4","Sheraton4", "Radisson4", "Gołębiewski"],
"Sanok":["Mercury5", "Novotel5","Sheraton5", "Radisson5", "Gołębiewski5"],
}
},
"Mazowieckie":{
"town":{
"Warszawa":["Mercury6", "Novotel6","Sheraton6", "Radisson6", "Gołębiewski6"],
"Sochaczew":["Mercury7", "Novotel7","Sheraton7", "Radisson7", "Gołębiewski7"],
"Płock":["Mercury8", "Novotel8","Sheraton8", "Radisson8", "Gołębiewski8"],
"Radom":["Mercury9", "Novotel9","Sheraton9", "Radisson9", "Gołębiewski9"],
"Ciechanów":["Mercury0", "Novotel0","Sheraton0", "Radisson0", "Gołębiewski0"],
}
}
}
}
var jsonData = JSON.parse(JSON.stringify(json));
console.log(typeof jsonData);
var region = document.getElementById("region");
var town = document.getElementById("town");
var hotel = document.getElementById("hotel");
let reg = Object.keys(jsonData);
cmo(reg, region);
setTown();
function setTown(){
town.innerHTML = "";
let towns = jsonData[region.value];
cmo(towns, town);
setHotel();
};
function setHotel(){
hotel.innerHTML = "";
let hotels = jsonData[region.value][town.value];
cmo(hotels, hotel);
};
function cmo(arr, s){
arr.forEach(o => {
let opt = document.createElement("option");
opt.value = o;
opt.innerHTML = o;
s.add(opt);
});
}
<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" type="text/css" href="./styles.css">
<title>Generowanie - wybór danych</title>
</head>
<body>
<p><label for = "region">Choose a region:</label>
<select name = "region" id = "region" onchange="setTown();">
<option disabled selected value="">Select a region</option>
<option value = "Warmińsko - mazurskie">Warmińsko - mazurskie</option>
<option value = "Małopolskie">Małopolskie</option>
<option value = "Podlaskie">Podlaskie</option>
<option value = "Podkarpackie">Podkarpackie</option>
<option value = "Mazowieckie">Mazowieckie</option>
</select>
</p>
<p>
<label for = "town">Choose a town:</label>
<select name = "town" id="town" onchange="setHotel();">
</select>
</p>
<p>
<label for="hotel" id="hotel">Choose a hotel:</label>
<select name="hotel" id="hotel">
</select>
</p>
<script type="text/javascript" src="./scripts.js"></script>
</body>
</html>
【问题讨论】:
标签: javascript