警报值与输入值相关的对象的属性

时间:2012-08-15 11:55:13

标签: javascript jquery

我无法提醒对应于区域标记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>

我做错了什么?

3 个答案:

答案 0 :(得分:2)

首先,尝试使用 console.log

替换警告
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/

答案 1 :(得分:1)

您的代码似乎在this jsFiddle example处于此处。我添加了另一个元素<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
});

答案 2 :(得分: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 ...因为那就是它的确切含义。