When clicking an option from a list, hide options from another select

Asked

Viewed 951 times

1

I would like, for example, when choosing the option '10 people' in the select 'Size', I could hide specific options in the selects 'Model', 'Fillings', 'Options to Drain' and 'Decoration'.

Example:

<div class="form-group">
    <span class="resume-subtitle">Escolha o tamanho</span>
    <select id="tamanho" name="tamanho" class="form-control select2-list select-options-1" data-placeholder="Clique aqui para escolher" multiple>
        <optgroup label="Tamanho">
        <?php

            $strTamanho = "SELECT * FROM bolo_mesa_itens WHERE tipo='tamanho'";

            $rsTamanho = mysql_query($strTamanho);

            while($rowTamanho = mysql_fetch_array($rsTamanho)) {
                echo '<option value="' . $rowTamanho['value'] . '">' . $rowTamanho['nome'] . '</option>';
            } 
        ?>      
        </optgroup>                                                 
    </select>
</div>

<div class="form-group">
<span class="resume-subtitle">Escolha o modelo</span>

<select id="modelo" name="modelo" class="form-control select2-list select-options-1" data-placeholder="Clique aqui para escolher" multiple>
<optgroup label="Tipo de Bolo">
<?php
    $strModelo = "SELECT * FROM bolo_mesa_itens WHERE tipo='modelo'";

    $rsModelo = mysql_query($strModelo);

    while($rowModelo = mysql_fetch_array($rsModelo)) {
        echo '<option class="10_pessoas" value="' . $rowModelo['value'] . '">' . $rowModelo['nome'] . '</option>';
    }
?>
</select>
</div>

2 answers

2

See an idea of what it would be like to solve this problem:

$('#tamanho').on('change', function() {
    var classe = this.value;
    var options = $('#modelo option').remove();
	options.each(function() {
        var opt = $(this);
        if (opt.hasClass(classe)) opt.show();
        else opt.hide();
    });
	$('#modelo').append(options);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- seleção com os tamanho-->
Tamanhos:
<select id="tamanho" name="tamanho">
    <option selected="selected" value="0">Escolha um tamanho</option>
    <option value="10">10 pessoas</option>
    <option value="20">20 pessoas</option>
    <option value="30">30 pessoas</option>
</select>
<br/>
<br/>
Modelos:
<!-- selção com modelos -->
<select id="modelo" name="modelo">    
    <option value="M1" class="10">Modelo 1</option>
    <option value="MO" class="10">Outro Modelo</option>
    <option value="MA" class="20">Modelo Aleatório</option>
    <option value="M3" class="30">Modelo 2</option>
    <option value="MA" class="30">Modelo 3</option>
    <option selected="selected" value="0">Escolha um modelo</option>
</select>

I created a file called Combodinamic and put it on Github just for the record.

  • It’s an excellent output, associating filtering to classes! But if my value is a common string like the one displayed, like "10 People"?

  • @Muriloravani has no problem. There you have to change the model class to the value that references the size.

  • Sorry @Ack, I don’t understand... is that in case, I can’t have a class with spaces like . 10 People (I didn’t quote there, but my value of this field is like this instead of 10_people. You can exemplify for me?

  • Buddy, I just finished testing the environment, and your code makes the filter right. When I adapt to mine ends up not doing... is it because I’m using the plugin Select2? I updated the post code to match mine. As a test, I also forced all the 'model' options to stay with the '10_people' class (which is the value that comes now from the database instead of 10 People).

  • @Muriloravani Cara answering the first question there, good even being separate value in class, works here for me. Now your adaptation, there’s no way I can play it. Did you insert jQuery correctly? Without it it won’t even work.

  • @Muriloravani I will analyze here to see if there are any mistakes.

  • I can give you access to see online too if you want :)

Show 3 more comments

0


The @Ack solution works perfectly in virtually any situation. Below I am putting a solution that would work specifically in my case, I am using the plugin Select2 in my form:

http://jsfiddle.net/harveyramer/SY47J/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.