0
希望有人能幫助我。下面的JS,加載一個JSON文件並將這些縣分析到一個選擇菜單中。它也刪除重複項。現在在JSON飼料,每個項目都有這樣的事情:嘗試訪問json對象時出現'找不到變量'錯誤
{
"County":"Antrim",
"Town":"Antrim Town",
"Restaurant":"Diane's Restaurant & Pizzeria"
}
我所試圖做的是在第一選擇菜單,一旦用戶選擇了縣委,第二選擇菜單與來自值更新兒子對象。目前,我得到一個'無法找到變量'的錯誤,我無法解決原因。由於某種原因數據數組不可用?
<script type="text/JavaScript">
$(document).ready(function(){
//get a reference to the select elements
var county = $('#county');
var town = $('#town');
var restaurant = $('#restaurant');
//request the JSON data and parse into the select element
$.getJSON('rai.json', function(data){
console.log(data);
//clear the current content of the select
$('#county').html('');
$('#county').append('<option>Please select county</option>');
$('#county').html('');
$('#town').append('<option>Please select town</option>');
$('#restaurant').html('');
$('#restaurant').append('<option>Please select restaurant</option>');
//iterate over the data and append a select option
$.each(data.irishtowns, function(key, val) {
county.append('<option id="' + val.County + '">' + val.County+ '</option>');
});
var a = new Array();
$('#county').children("option").each(function(x){
test = false;
b = a[x] = $(this).text();
for (i=0;i<a.length-1;i++){
if (b ==a[i]) test =true;
}
if (test) $(this).remove();
});
});
$("#county").change(function() {
var myCounty = $(this).val();
console.log(myCounty);
$.each(data.irishtowns, function(key, val) {
if (val.Town === myCounty) {
town.append('<option id="' + val.Town + '">' + val.Town + '</option>');
}
});
});
});
</script>
什麼是確切的錯誤信息?它發生在哪一行的Javascript? – Barmar
控制檯是否識別腳本中哪個變量無法找到? – Korgrue
錯字,複製錯誤或什麼是'data.irishtowns'?當你轉儲json時,它是一個對象還是一個字符串? –