我不明白我做错了什么?我认为我已经写得很好:
HTML 代码如下:
<b> Select Area</b>
<select id="mySelect_1" onchange="showSelectedArea();" >
<option selected disabled hidden value=''></option>"
<option value="1">Center</option>
<option value="2">New jersey</option>
</select>
Javascript 是这样的:
layer1.setVisibility(false);
layer2.setVisibility(false);
layer3.setVisibility(false);
layer4.setVisibility(false);
}
function showSelectedArea() {
var e = document.getElementById("mySelect_1");
var valueEpilogi_1 = e.options[e.selectedIndex].value;
if (valueEpilogi_1 == "1") {
layer3.setVisibility(true);
}
}
我认为问题不在于 if 或传递值,如果我设置 true == true 它仍然不会将可见性设置为 true。我认为在选择标记处触发该函数存在问题。
请检查我的外部 js 文件并告诉我问题是什么?我在 init 函数中定义了在 body load 上运行的所有层?这是问题吗? snk.to/f-cdh90xd4
最佳答案
您必须读取选择尝试的值:
var valueEpilogi_1 = document.getElementById('mySelect_1').value ;
编辑: 如果您想在 init() 函数之外使用“layer”,则必须将其定义为全局变量
示例:
function init(){
/* this variable is global, declaration without 'var' before, so it can be used out of the function*/
perioxes = new OpenLayers.Layer.Vector("Polygon Layer");
...
map.addLayer(perioxes);
..
perioxes.setVisibility(false);
...
}
function showSelectedArea() {
var valueEpilogi_1 = document.getElementById('mySelect_1').value ;
if (valueEpilogi_1 == "1") {
layer.setVisibility(true);
}
}
关于javascript - 开放层 : how to turn on visibility of a layer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25036432/