2013-08-27 211 views
0
function test(results) { 
     //Populate the ComboBox with unique values 

     var Gov; 
     var values = []; 
     var features = results.features; 
     var og; 
     for (i = 0; i < features.length; i++) { 

      var aGOV = { 
       "GovName": features[i].attributes.ENG_NAME, 
       "GovNO": features[i].attributes.GOV_NO, 
       "Shape": features[i].geometry 
      } 
      og = new Option(features[i].attributes.ENG_NAME, aGOV); 
      var cbx = document.getElementById("cbxGov"); 
      cbx.options.add(og); 
     } 

    } 

    function gov_selection_change() 
    { 
     var cbx = document.getElementById("cbxGov"); 

     var itm = cbx.options[cbx.selectedIndex].value.hasOwnProperty("Shape"); 
    } 

HTML代码如何访问JavaScript对象属性

<select id="cbxGov" onchange="gov_selection_change()"> 

我的问题是我不是能够访问我的gov_selection_change()功能aGOV的属性时,显示它有没有这样的属性,ITM是假的。

+0

看起来你实际上并没有试图在'goc_selection_change'中访问'aGOV'的任何属性,对吗?如果你这样做,这将超出范围。在'test'之外定义'aGOV'。 – apsillers

回答

0

一个HTMLOptionElement的值属性总是返回一个DOMString(也称为文本),而不是一个对象。

因此,您必须在查找字典中保存要访问的内容,然后使用返回的值作为查找键。

var lookupDictionary = {}; 

function test(results) { 
    var lookupKey, 
     og; 
    //... 

    // don´t get the element in the loop 
    var cbx = document.getElementById("cbxGov"); 

    //... 

    for (i = 0; i < features.length; i++) { 
     lookupKey = features[i].attributes.GOV_NO; 

     lookupDictionary[lookupKey] = { 
      "GovName": features[i].attributes.ENG_NAME, 
      "GovNO": features[i].attributes.GOV_NO, 
      "Shape": features[i].geometry 
     } 

     og = new Option(features[i].attributes.ENG_NAME, lookupKey); 
     cbx.options.add(og); 
    } 
} 

function gov_selection_change() { 
    var cbx = document.getElementById("cbxGov"); 
    var key = cbx.options[cbx.selectedIndex].value; 
    var itm = lookupDictionary[key].hasOwnProperty("Shape"); 
} 
+0

非常感谢,我会试试这个 – anikas

+0

哇它的工作非常感谢 – anikas

+0

hye我需要更多的帮助。你能告诉我如何使用javascript从