2013-08-25 73 views
0
的JSON代碼

蔭newbi我將不勝感激,如果有人能幫助我這個錯誤: 我有汽車模型+年份+存款+價格Json的下拉列表中錯誤

當用戶第一次點擊模型不是選擇一年存款顯示每月還款額。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html> 
<head> 
<title>Cars</title> 
<link href="css/cakeform.css" rel="stylesheet" type="text/css" /> 
<script> 
var prices = { 
'model' : 'Skoda' { 
'1' : { 
    '12300' : '959', 
    '13000' : '892', 
    '13500' : '844', 
    '19000' : '317' 
}, 
'2' : { 
    '12300' : '542', 
    '13000' : '504', 
    '13500' : '477', 
    '19000' : '179' 
}, 
'3' : { 
    '12300' : '403', 
    '13000' : '375', 
    '13500' : '355', 
    '19000' : '133' 
} 
}; 
    'Tiida' { 
'1' : { 
    '12300' : '955', 
    '13000' : '892', 
    '13500' : '844', 
    '19000' : '317' 
}, 
'2' : { 
    '12300' : '555', 
    '13000' : '504', 
    '13500' : '477', 
    '19000' : '179' 
}, 
'3' : { 
    '12300' : '455', 
    '13000' : '375', 
    '13500' : '355', 
    '19000' : '133' 
} 
}; 
}; 
    onload = function(){ 
var f = document.booklets; 
var model = f.model; 
var qtyEl = f.qty; 
var pagesEl = f.pages; 
var priceEl = document.getElementById('price'); 
f.qty.onchange = f.pages.onchange = f.model.onchange = function(){ 
    var qty = qtyEl[qtyEl.selectedIndex].value; 
    var model = modelEl[modelEl.selectedIndex].value; 
    var pages = pagesEl[pagesEl.selectedIndex].value 
    priceEl.innerHTML = prices[pages][qty][model]; 
}; 
f.qty.onchange();//initialise price field for whatever values are selected on page load 
}; 
</script> 
</head> 
<body> 

<fieldset> 
<legend>Cars</legend> 
<form name="booklets" method="" action=""> 
<div>Select Model: 
<select class="selectClass" name="model"> 
    <option value="Skoda">Skoda</option> 
    <option value="Tiida">Tiida</option> 
    <option value="Tyota">Toyota</option> 
</select> 
<div>Select Year: 
<select class="selectClass" name="pages"> 
    <option value="1">1 Year</option> 
    <option value="2">2 Years</option> 
    <option value="3">3 Years....</option> 
</select> 
<br><br>Select Deposit: 
<select class="selectClass" name="qty"> 
    <option value="12300">$12300</option> 
    <option value="13000">$13000</option> 
    <option value="13500">$13500</option> 
</select> 
    </div> 
    <div><br>Monthly Payment: <font color="blue" size=7>$ <span id="price"></span> </font>  </div> 
    </form> 
</fieldset> 
</body> 
</html> 

在此先感謝:

回答

1

您的JSON是無效的,因此,必須予以固定。您應該使用"而不是'獲取有效的JSON,並使用類似JSONLint的內容來檢查錯誤的逗號和括號。

固定JSON變量:

var prices = { 
    "Skoda": { 
     "1": { 
      "12300": "959", 
      "13000": "892", 
      "13500": "844", 
      "19000": "317" 
     }, 
     "2": { 
      "12300": "542", 
      "13000": "504", 
      "13500": "477", 
      "19000": "179" 
     }, 
     "3": { 
      "12300": "403", 
      "13000": "375", 
      "13500": "355", 
      "19000": "133" 
     } 
    }, 
    "Tiida": { 
     "1": { 
      "12300": "955", 
      "13000": "892", 
      "13500": "844", 
      "19000": "317" 
     }, 
     "2": { 
      "12300": "555", 
      "13000": "504", 
      "13500": "477", 
      "19000": "179" 
     }, 
     "3": { 
      "12300": "455", 
      "13000": "375", 
      "13500": "355", 
      "19000": "133" 
     } 
    } 
}; 

然後你在你的代碼有一個或兩個錯字,你是不正確的訪問JSON數據。您必須確保根據結構中的層次結構訪問它。這裏的意思是prices[model][pages][qty]

固定onload功能:

window.onload = function() { 
    var f = document.booklets; 
    var modelEl = f.model; 
    var qtyEl = f.qty; 
    var pagesEl = f.pages; 
    var priceEl = document.getElementById('price'); 
    f.qty.onchange = f.pages.onchange = f.model.onchange = function() { 
     var qty = qtyEl[qtyEl.selectedIndex].value; 
     var model = modelEl[modelEl.selectedIndex].value; 
     var pages = pagesEl[pagesEl.selectedIndex].value; 
     priceEl.innerHTML = prices[model][pages][qty]; 
    }; 
    //initialise price field for whatever values are selected on page load 
    f.qty.onchange(); 
}; 

DEMO:http://jsfiddle.net/eGQr2/

+0

它的了不起的人。非常感謝 – user1710911