Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中访问对象属性 功能测试(结果){ //用唯一值填充组合框 var-Gov; var值=[]; var features=结果。特征; var-og; 对于(i=0;i_Javascript_Html - Fatal编程技术网

如何在javascript中访问对象属性 功能测试(结果){ //用唯一值填充组合框 var-Gov; var值=[]; var features=结果。特征; var-og; 对于(i=0;i

如何在javascript中访问对象属性 功能测试(结果){ //用唯一值填充组合框 var-Gov; var值=[]; var features=结果。特征; var-og; 对于(i=0;i,javascript,html,Javascript,Html,html代码 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

html代码

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


我的问题是,我无法在我的
gov\u selection\u change(
)函数中访问aGOV的属性,这表明它没有此类属性,itm为false

变量
aGOV
仅在
result()
函数的上下文中可用。如果要从其他函数使用它,请将其声明为全局变量

例如:

<select id="cbxGov" onchange="gov_selection_change()">
的value属性始终返回DOMString(又称文本),而不是对象

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

var aGOV;

function result()
{
    // initialize aGOV
}

function gov_selection_change()
{
    // now you can access the aGOV variable
}
var lookupDictionary={};
功能测试(结果){
var lookupKey,
og;
//...
//不要在循环中获取元素
var cbx=document.getElementById(“cbxGov”);
//...
对于(i=0;i
看起来您实际上并没有试图访问
goc\u selection\u change
中的
aGOV
的任何属性,是吗?如果你这样做了,它将超出范围。定义
aGOV
test
之外。非常感谢,我将尝试此方法。我需要更多帮助。您能告诉我如何从使用javascript中删除该选项吗。我需要再加满
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");
}