使用另一个节点的值查询JSON节点值

时间:2012-01-04 01:52:08

标签: javascript json

JSON新手,所以我会尽力而为。我有一个名为HUDS的JSON对象。下面是2个示例节点(顺便说一句,我可以像在XML中一样用JSON调用这些节点吗?)。

var HUDS = [
{
    "DISTRICT": "100",
    "BIOS": "BROWN",
    "AREA_KM": "3663.158164",
    "AREA_MI": "1414.347616",
    "NAME": "100",
    "REG": "1",
    "ACRES": "905182",
    "EMU_Name": "Purcell",
    "Shape_Leng": "299746.4938",
    "Shape_Area": "3663158164",
},
{
    "DISTRICT": "101",
    "BIOS": "THIER",
    "AREA_KM": "1507.774765",
    "AREA_MI": "582.152762",
    "NAME": "101",
    "REG": "1",
    "ACRES": "372578",
    "EMU_Name": "Salish",
    "Shape_Leng": "229150.0655",
    "Shape_Area": "1507774766",
}

我有一个下拉表单,用于指定特定于我的JSON中“BIOS”字段的值。例如,用户可以从下拉菜单中选择BROWN。

然后我想创建一个可以用来填充div的var。例如,当用户选择BROWN时,我希望我的div填充“EMU_Name”中的值

我知道这是错的,但也许它传达了我的目标

function dropDownAction(){
var tempBios=document.BIOSForm.BIOS.value;
var tempEmuValue=HUDS.BIOS==tempBios.EMU_Name;
document.getElementById("mydiv").innerHTML=tempEmuValue;

}

1 个答案:

答案 0 :(得分:1)

尝试:

    <form name="BIOSForm">
        <select name="BIOS" onchange="dropdownaction();">
            <option value="BROWN">BROWN</option>
            <option value="THEIR">THEIR</option>
        </select>
    </form>
    <div id="emuname">

    </div>

    <script type='text/javascript'>
    var HUDS = [
    {
        "DISTRICT": "100",
        "BIOS": "BROWN",
        "AREA_KM": "3663.158164",
        "AREA_MI": "1414.347616",
        "NAME": "100",
        "REG": "1",
        "ACRES": "905182",
        "EMU_Name": "Purcell",
        "Shape_Leng": "299746.4938",
        "Shape_Area": "3663158164",
    },
    {
        "DISTRICT": "101",
        "BIOS": "THIER",
        "AREA_KM": "1507.774765",
        "AREA_MI": "582.152762",
        "NAME": "101",
        "REG": "1",
        "ACRES": "372578",
        "EMU_Name": "Salish",
        "Shape_Leng": "229150.0655",
        "Shape_Area": "1507774766",
    }
    ]

    function dropdownaction(){
        for(var x=0;x<HUDS.length;x++){
            var tempBios = document.BIOSForm.BIOS.value;
            if(tempBios == HUDS[x].BIOS){
                document.getElementById("emuname").innerHTML = HUDS[x].EMU_Name;
                break;
            }
        }

    }


    </script>