Pergunta

Não consigo entender o que estou fazendo de errado?Acho que escrevi tudo muito bem:

O código HTML é assim:

    <b>&nbspSelect 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>

e o Javascript é este:

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

    }

Não acho que o problema seja com if ou com a passagem do valor, se eu definir true == true, ele ainda não definirá a visibilidade como true. Acho que há um problema ao acionar a função na tag select.

Por favor, verifique meu arquivo js externo e me diga qual é o problema?Eu defino todas as camadas na função init que é executada na carga do corpo. Esse é o problema?snk.to/f-cdh90xd4

Foi útil?

Solução

Você deve ler o valor do select e tentar isso:

var valorEpilogi_1 = document.getElementById('mySelect_1').value ;

editar:Se você quiser usar 'layer' fora de sua função init(), ela deve ser definida como variável global

exemplo :

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

}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top