0

我无法提醒与区域标签 ID 对应的对象值。

我的代码是,

          $(document).ready(function () {
            $(".wholecirclewrapper area").click(function(){      
                if($(this).hasClass("active")) {        
                    return false;
                }       
                var choice = $(this).attr("id");    
                $.each(results.States, function(i, item) {
                    var st=results.States[i].Name;
                    if(choice==st)
                    {
                        alert(results.States[i].ISHI);  
                    }
                });

            });

        }); 

我的 HTML 代码是

            <div id="mapdiv">
        <img src="india.gif"  alt="India" usemap="#indiamap" />
        <map name="indiamap" class="wholecirclewrapper">
            <area class="specificshelfdiv1" id="AndraPradesh" shape="rect" coords="0,0,82,126" href="#"  />
            <area class="specificshelfdiv2" id="ArunachalPradesh" shape="circle" coords="90,58,3" href="#"  />
            <area class="specificshelfdiv3" id="Assam" shape="circle" coords="124,58,8" href="#"   />     
        </map> 
    </div>

我的外部 json 看起来像

      var results={"States":[
        { "Name" : "AndraPradesh" , "ISHI" : "19.5 (Serious)" ,"Literacy rate" : "67.7" ,"Population rate" : "84,665,533" ,"NFHS" : "29.8" ,"Tap water" : "69.9"}, 
        { "Name" : "ArunachalPradesh" , "ISHI" : "Not estimated" ,"Literacy rate" : "67" ,"Population rate" : "1,382,611" ,"NFHS" : "29.6" ,"Tap water" : "65.5"}, 
        { "Name" : "Assam" , "ISHI" : "19.8 (Serious)" ,"Literacy rate" : "73.2" ,"Population rate" : "31,169,272" ,"NFHS" : "35.8" ,"Tap water" : "10.5"} 
     ]
     };

我通过调用 json

           <script src="json_states.json" type="text/javascript"> </script>

我究竟做错了什么?

4

3 回答 3

2

首先,尝试用console.log替换alert

console.log(results.States[i].ISHI);  

然后在 Chrome 中按 F12,转到控制台并查看是否打印了值。根据经验,始终使用 console.log 而不是 alert,因为它更强大且更易于使用。

更新:

尝试在文档头中按以下顺序加载脚本:

<script src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<script src="json_states.json"></script>
<script>
   $(document).ready(function () {
...
</script>

另请参阅我写的这个例子:http ://radulescu.me/overflow/1/

于 2012-08-15T11:59:19.557 回答
1

您的代码似乎在此 jsFiddle 示例中有效。我正在添加另一个元素<p id="result"></p>,而不是提醒我正在设置.text()这个元素,这很好。试试这个方法alert,看看你得到了什么。

$(".wholecirclewrapper area").click(function() {
    if ($(this).hasClass("active")) {
        return false;
    }

    var choice = $(this).attr("id");

    $.each(results.States, function(i, item) {
        var st = results.States[i].Name;
        if (choice == st) {
            $("#result").text(results.States[i].ISHI);
        }
    });
});

您是否使用某种弹出窗口阻止程序,因为这些阻止某些脚本执行。如果没有,那么我建议您在浏览器调试环境中检查脚本错误。


编辑- 如果您从外部文件加载 json,而不是将其包含在标题中,您是否尝试像这样加载它:

$.getJSON('json_states.json', function(data) {
    var results = data;

    // do the rest of your script here
});
于 2012-08-15T12:07:28.203 回答
1

已经说了3次了,但是你好像忽略了,或者不理解,我再发一次。

var results={"States":[
    { "Name" : "AndraPradesh" , "ISHI" : "19.5 (Serious)" ,"Literacy rate" : "67.7" ,"Population rate" : "84,665,533" ,"NFHS" : "29.8" ,"Tap water" : "69.9"}, 
    { "Name" : "ArunachalPradesh" , "ISHI" : "Not estimated" ,"Literacy rate" : "67" ,"Population rate" : "1,382,611" ,"NFHS" : "29.6" ,"Tap water" : "65.5"}, 
    { "Name" : "Assam" , "ISHI" : "19.8 (Serious)" ,"Literacy rate" : "73.2" ,"Population rate" : "31,169,272" ,"NFHS" : "35.8" ,"Tap water" : "10.5"} 
 ]
 };

不是 JSON。它是一个 Javascript 对象。

JSON 就是这样:

{"States":[
    { "Name" : "AndraPradesh" , "ISHI" : "19.5 (Serious)" ,"Literacy rate" : "67.7" ,"Population rate" : "84,665,533" ,"NFHS" : "29.8" ,"Tap water" : "69.9"}, 
    { "Name" : "ArunachalPradesh" , "ISHI" : "Not estimated" ,"Literacy rate" : "67" ,"Population rate" : "1,382,611" ,"NFHS" : "29.6" ,"Tap water" : "65.5"}, 
    { "Name" : "Assam" , "ISHI" : "19.8 (Serious)" ,"Literacy rate" : "73.2" ,"Population rate" : "31,169,272" ,"NFHS" : "35.8" ,"Tap water" : "10.5"} 
 ]}

我不确定 .json 文件扩展名是否会导致任何浏览器出现问题,但如果您在文件中定义一个变量,它应该只是 .js ... 因为它就是这样。

于 2012-08-15T13:14:33.787 回答