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