Remove text from a select with JQuery

0

Can you help me with a function that removes texts from two combos? The combos are the following:

The problem is that by eliminating the selection of the Country combo, the other combos (State and Municipality) must be cleaned, that is to say, Lima and San Isidro should no longer show. I already try and the only thing that works for me is to detect that Country is already empty. I do not know what attributes I should use to not show me the texts. And this is the code:

jQuery(document).ready(function(){

    $('#tblempleados-pais').on('change', function(){

        if($('#tblempleados-pais').val() == '' || $('#tblempleados-pais').val() == null){

            alert('entra');
            $('#tblempleados-estado').val('');
            $('#tblempleados-municipio').val('');

        }

    });

});
    
asked by Ivan92 07.02.2018 в 19:52
source

2 answers

2

Thanks to everyone and I was right Camilo Vasquez was for the bookstore the answer was simple in the same way but I never thought that I would run into that. It is to replace this:

$('#tblempleados-estado').val('');
$('#tblempleados-municipio').val('');

Because of this:

$("#tblempleados-estado").select2("val", "");
$("#tblempleados-municipio").select2("val", "");
    
answered by 07.02.2018 / 22:29
source
1

Without modifying your JavaScript, add a blank option to each select. Example:

jQuery(document).ready(function(){

    $('#tblempleados-pais').on('change', function(){

        if($('#tblempleados-pais').val() == '' || $('#tblempleados-pais').val() == null){

            alert('entra');
            $('#tblempleados-estado').val('');
            $('#tblempleados-municipio').val('');

        }

    });

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
País <select type="text" id="tblempleados-pais"><br/>
<option></option>
<option selected="selected" value="Peru">Perú</option>

</select>
<br/>
Estado <select type="text" id="tblempleados-estado">
<option></option>
<option selected="selected" value="Lima">Lima</option>
</select>
<br/>
Municipio <select type="text" id="tblempleados-municipio">
<option></option>
<option selected="selected" value="San Isidro">San Isidro</option>
</select>
<br/>
    
answered by 07.02.2018 в 20:27