Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

119
Visualizações
Show div if select option item is selected. Problem with reloading

Hi I have HTML bootstrap code:

$(document).ready(function() {
  $("#us-state").find('select').change(function() {
    var $val = $(this).val();

    if ($val === 'US') {
      $('.us-state-select').show();
    } else {
      $('.us-state-select').hide();
    }
  });
});
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

<div class="col-sm mb-5  us-state-select">
  <label>USA STATE</label>
  <select name="order_from_address_state" class="form-select">
    <option>Choose country</option>
    <option value="AL" name="<AL>" selected>Alabama</option>
    <option value="AK" name="AK">Arkansas</option>
  </select>
</div>


<div class="col-sm mb-5" id="us-state"><label>Send from country</label>
  <select name="order_from_address_country" class="form-select">
    <option>Send from</option>
    <option value="US" name="US">USA</option>
    <option value="CA" name="CA" selected>Canada</option>
  </select>
</div>

(Also available on JSFiddle)

My problem is simple. I am using foreach to show all countries and statesm but... check the jsfiddle. if country is selected different to the US and its load with this different country, it shows div us-state-select and if u click on different countries, it dissapears and works as expected. But I dont want to show div with states in the case that page is loaded and its selected different country than US. How to do that? its obvious on fiddle, I selected canada and on page load there is div with states and when click on different value for example: Send from at country box, states dissapear and it works as expected then. Please, can you give me some advice? I am newbie at jquery, thx.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Change your JS to be like this: I am basically checking this logic once the page is ready and I also added some refactor in your code to help readability

$(document).ready(function() {
const checkValues = () => {
        var value = $("#us-state").find('select').val();
    console.log(value)
    if(value === 'US'){
        $('.us-state-select').show();
    }else{
        $('.us-state-select').hide();
    }
}

        $("#us-state").ready(checkValues);
    $("#us-state").find('select').change(checkValues);
});

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda