(eu qui fiz esse banner hihihi)

Ativando options disabled no IE

Ops, uma funçãozinha que eu tinha feito a tempos mas tinha esquecido de postar aqui heheh. Só tinha postado no Webly.

Se você colocar um atributo “disabled” em um option ele deverá ficar desabilitado, ou seja, indisponível. Exemplo:


<select>
    <option>opt 1</option>

    <option disabled='disabled'>opt 2</option>
    <option>opt 3</option>
</select>

Isto acontece bem nos navegadores padrão.
No nosso velho amigo IE (Internet Explorer) não acontece. Teste e veja.

Inspirado pela dúvida do nosso amigo Rafael, fui atrás do problema e não achei solução a não ser fazer uma função pra fazer o serviço completo.
Está abaixo:


<!--[if lte IE 7]>
<script>
function ativaOptionsDisabled(){
    var sels = document.getElementsByTagName('select');
    for(var i=0; i < sels.length; i++){
        sels[i].onchange= function(){ //pra se mudar pro desabilitado
            if(this.options[this.selectedIndex].disabled){
                if(this.options.length<=1){
                    this.selectedIndex = -1;
                }else if(this.selectedIndex < this.options.length - 1){
                    this.selectedIndex++;
                }else{
                    this.selectedIndex--;
                }
            }
        }
        if(sels[i].options[sels[i].selectedIndex].disabled){
            //se o selecionado atual é desabilitado chamo o onchange
            sels[i].onchange();
        }
        for(var j=0; j < sels[i].options.length; j++){ //colocando o estilo
            if(sels[i].options[j].disabled){
                sels[i].options[j].style.color = '#CCC';
            }
        }
    }
}
window.attachEvent("onload", ativaOptionsDisabled)

</script>
<![endif]-->

Pronto! :)
Obs.: Esta Esta função substitui algum outro evento ONCHANGE que tenha sido colocado antes pra algum option.

Leave a Reply