猿问

jQuery的getJSON方法

<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");

都没有执行


taba
浏览 1106回答 1
1回答

涂涂023

修改下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>");                         });                           });希望采纳,有问题可 回复

慕瓜9220888

DEJVAGQCCVMUGOORXUPWIXPITTWINWFVVWYOBEUMVOHKNKRDITDZJGHYJUTGPSMARJVDBLOXGNWBWMTPZCCRJVLYHHJGPHSPYEEITUIKHDAJGQFZEBEDQEYWNFIYVUVOMFGKNTGQRTQIRBKHABQACCFYLPGFEWPKKANFGFRUTLXPQYHWYIRGULDSYXUDHKBQCHATKBOZRFLUNHKHYURDFZZZLCBZLLYKEJKWRTSLVHXLDFKTWPCGQVNWPITDUVACXXUMCHYQHTJCCVTKTHQJMYOVFUMBIRKOWOJUJTWSCCALASFOOGFAV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