<html?xmlns="http://www.w3.org/1999/xhtml">
????<head>
????????<title>使用getJSON()方法異步加載JSON格式數據</title>
????????<script?src="http://libs.baidu.com/jquery/1.9.0/jquery.js"?type="text/javascript"></script>
????????<link?href="style.css"?rel="stylesheet"?type="text/css"?/>
????</head>
????
????<body>
????????<div?id="divtest">
????????????<div?class="title">
????????????????<span?class="fl">我最喜歡的一項運動</span>?
????????????????<span?class="fr">
????????????????????<input?id="btnShow"?type="button"?value="加載"?/>
????????????????</span>
????????????</div>
????????????<ul></ul>
????????</div>
????????
????????<script?type="text/javascript">
????????????$(function?()?{
????????????????$("#btnShow").bind("click",?function?()?{
????????????????????var?$this?=?$(this);
????????????????????$.getJSON('sport.json',function(data){//回調函數
????????????????????????$this.attr("disabled",?"true");
????????????????????????$.each(data,?function?(index,?sport)?{
????????????????????????????$("ul").append("<li>"?+?sport["name"]?+?"</li>");
????????????????????????});
????
????????????????????});
????????????????})
????????????});
????????</script>
????</body>
</html>下面是sport.json文件里面的東西[{{"name" : "足球"},{"name" : "籃球"},{"name" : "乒乓球"}}]點擊加載之后,毫無反應,連$this.attr("disabled",?"true");都沒有執行
1 回答
已采納
涂涂023
TA貢獻8條經驗 獲得超8個贊
修改下JSON和代碼 ,因為你的json代碼不是標準的json格式
json修改為
{
????"result":[
????????{"name"?:?"足球"},
????????{"name"?:?"籃球"},
????????{"name"?:?"乒乓球"}
?????]
}代碼在23行開始修改為
$.getJSON('sport.json',function(data){//回調函數
????????????????????????$this.attr("disabled",?"true");
????????????????????????$.each(data.result,?function?(index,?sport)?{
????????????????????????????$("ul").append("<li>"?+?sport["name"]?+?"</li>");
????????????????????????});
?????
????????????????????});希望采納,有問題可 回復
- 1 回答
- 0 關注
- 1204 瀏覽
添加回答
舉報
0/150
提交
取消
