[英]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.