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

132
Visualizações
Hide div , when default option is chosen

I'm currently trying to do a selection with three options: -The first, is the default one. -the 2nd option displays inputs, -and the 3rd other inputs

My problem is: that when I choose the second or the third option, my inputs change, so that's good. But when I select the default one, my inputs that were previously selected do not disappear.

There is my code snippet:

$(document).ready(function() {
    $('#bat').change(function() {
        $($(this).children("option:selected").attr("data-hide")).hide();
        $($(this).children("option:selected").attr("data-show")).show();
     $($(this).children("option[value=1]").attr("data1")).hide();
     $($(this).children("option[value=1]").attr("data2")).hide();
    });
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="bat" name="card_name" class="form-select">
    <option data1='#access' data2='#amount' value="1">--Veuillez choisir une option--</option>
    <option data-hide='#access' data-show='#amount' value="A">Fichier separes : plusieurs fichiers de une page
    </option>
    <option data-hide='#amount' data-show='#access' value="B">Fichier multipages : 1 fichier de plusieurs pages
    </option>
</select>

<div id="access">div 1</div>
<div id="amount">div 2</div>

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

0

You can use the same method as you used before and create a div around both options:

$(document).ready(function() {
    $('#bat').change(function() {
      $('#results').show()
      $($(this).children("option:selected").attr("data-hide")).hide();
      $($(this).children("option:selected").attr("data-show")).show();
    });
  })
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="bat" name="card_name" class="form-select">
          <option data-hide='#results' data1='#access' data2='#amount' value="1">--Veuillez choisir une option--</option>
          <option data-hide='#access' data-show='#amount' value="A">Fichier separes : plusieurs fichiers de une page
          </option>
          <option data-hide='#amount' data-show='#access' value="B">Fichier multipages : 1 fichier de plusieurs pages
          </option>
        </select>
<div id="results">
<div id="access" style="display:none;">div 1</div>
<div id="amount" style="display:none;">div 2</div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Both Hide and show occur simultaneously that's why when you select the default option value both div do not show. Just pull up below two lines of code it will work fine. I keep your HTML as it was

  $(document).ready(function() {
    $('#bat').change(function() {          
    $($(this).children("option:selected").attr("data2")).show();
    $($(this).children("option:selected").attr("data1")).show();

    $($(this).children("option:selected").attr("data-hide")).hide();
    $($(this).children("option:selected").attr("data-show")).show();
    });
})
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