简体   繁体   中英

Function with if else condition in javascript ever returns true

I have the function below:

$("#botao").on("click", function(){
    s = $("#municipio").val();
    alert(s);
    if (s == 1){
        $("#imagem").attr("src","https://gatolovers.com.br/uf/popup1.png");
        $("#mensagem").html('Uhul! Nós atendemos sua região. Temos uma unidade de atendimento pertinho de você. Ligue agora mesmo e faça um orçamento.');
    }else{
        $("#imagem").attr("src","https://gatolovers.com.br/uf/popup0.png");
        $("#mensagem").html('Ops! Ainda não estamos na sua cidade. Mas muito em breve levaremos a melhor empresa de Redes de Proteção para sua região, consulte nosso plano de expansão.');
    }
});

Independs of s be 0 or 1, the function ever return the first block.

The problem are the div #mensagem and #imagem that not work correctly. The admin can delete this post.

How do I solve the problem?

Try with replacing your if condition as : if(s===1)

$("#botao").on("click", function(){
    s = $("#municipio").val();
    alert(s);
    if (s === 1){
        $("#imagem").attr("src","https://gatolovers.com.br/uf/popup1.png");
        $("#mensagem").html('Uhul! Nós atendemos sua região. Temos uma unidade de atendimento pertinho de você. Ligue agora mesmo e faça um orçamento.');
    }else{
        $("#imagem").attr("src","https://gatolovers.com.br/uf/popup0.png");
        $("#mensagem").html('Ops! Ainda não estamos na sua cidade. Mas muito em breve levaremos a melhor empresa de Redes de Proteção para sua região, consulte nosso plano de expansão.');
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM