[英]Get values of selected checkboxes and their associated textareas in Javascript
[英]How to get values associated to a javascript dictionary
我在 html 页面的脚本标签内有这个声明的字典变量。 我想获取与“Roads”和“Intersections”键关联的值。 每次刷新页面时,这些值都会更改。 捕捉它们,将允许我使用 Javascript 在我的 python folium 地图上更改这些颜色:
(geo_json_cae0ea33c63e4c438678d293e5c32c0d.setStyle({'fillColor': "#FF0000", 'color': "#FF0000"});
根据建议,我尝试了此操作,但无法获得预期的结果。
var layer_control_aec3ac6e0e424b74a19b4c9d1c78ffeb = {
base_layers : {
},
overlays : {
"Roads" : geo_json_cae0ea33c63e4c438678d293e5c32c0d,
"Intersections" : feature_group_1c28eff59e394734be54cf676a09eae1,
},
};
window.onload = function() {
for (var name in this) {
if (name.includes("layer_control_")) {
for(let key in this[name]) {
console.log(key, this[name][key])
}
}
}
};
除了我想要的东西(覆盖字典的值)之外,我在控制台中得到了很多东西
假设您无法直接访问layer_control_aec3ac6e0e424b74a19b4c9d1c78ffeb
(因为重新加载时名称发生变化),您可以从window
访问该变量:
var layer_control_aec3ac6e0e424b74a19b4c9d1c78ffeb = { base_layers : { }, overlays : { "Roads" : "geo_json_cae0ea33c63e4c438678d293e5c32c0d", "Intersections" : "feature_group_1c28eff59e394734be54cf676a09eae1", }, }; window.onload = function(){ for (const name in this){ if (name.includes("layer_control_")){ let { Roads, Intersections } = window[name].overlays; console.log(Roads, Intersections); // Roads.setStyle({'fillColor': "#FF0000", 'color': "#FF0000"}); // ... } } };
您对for (var name in this)
初始方法是正确的。
但是, name
只包含变量的名称(字符串),而不是它的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.