文件 「zip.json」: { 「zip11111」: 「City1」, 「zip99999」: 「城2」 }如何正確返回並顯示json數據和getJSON?
的JavaScript:
jQuery(document).ready(function() {
$("#textbox1zip").change(function() {
$.getJSON('zip.json', function(data) {
/* each of the following code blocks don't work when exchanged for each other:
1 zipcode = "zip" + $("#textbox1zip").val();
* $('#textbox2city').val(data.zipcode); -----> call silently dropped
2 zipcode = 'zip99999';
* $('#textbox2city').val(data.zipcode); -----> call silently dropped
3 zipcode = "zip" + $("#textbox1zip").val();
* alert(data.zipcode); -----> returns 'undefined'
*/
/* but each of these blocks here works:
4 $('#textbox2city').val(data.zip99999); ------> ok
5 alert(data.zip99999); -----> ok
6 zipcode = 'zip99999';
* $('#textbox2city').val(zipcode); ------> ok
7 zipcode = "zip" + $("#textbox1zip").val();
* $('#textbox2city').val(zipcode); ------> ok
*/
});
});
});
我想從獲取的值textbox1zip將適當的值從「zip.json」返回到textbox2city。我想用getJSON。這也可能是很簡單的,但我不能看到它... ...
只需使用'console.log(data)'查看返回的具體內容,然後根據該對象進行編寫 – SpYk3HH 2013-04-24 20:35:37
我會添加括號。同樣謝謝,雖然! – user2317194 2013-04-24 20:52:30