clean a select in jquery

0

When making a query through ajax, the parameters that I am sending are sent by selects but the problem is that once the query is done the values remain there and they do not want to erase me, I used the option empty() but when I do, the options are erased and I do not want that, you could help me in this please, I leave the html and javascript code

                 <div class="form-group">
                    <select name="" id="select_granjas" class="form-control">
                        <option value="" selected="selected">Seleccione Granja: </option>
                        @foreach($granjas as $granja)
                            <option value="{{$granja->id}}">{{$granja->nombre_granja}}</option>
                        @endforeach
                    </select>
                </div>

that foreach is by laravel, but I just need javascript to clean that select but not to erase the data inside.

var array_granjas = [];
$('#select_granjas').on('change', function(e){
        array_granjas.push($(e.currentTarget).val())
        console.log('value_select_prueba: --------->', array_granjas);
})

in this array I am saving multiple values of that select and here below I call it for the ajax

$ ("# elaborar_grafica_granjas_conversion_final"). click (function () {             var datachart = new Array ();             var token = $ ("# token"). val ();             var a_granjas = $ ("# select_granjas_conversion_final"). val ();             var month = $ ("# select_granjas_mes"). val ();

        var json = {
            annio_granjas:a_granjas,
            granjas:array_granjas,
            mes:mes
        }

        console.log(json);

        if (a_granjas == '') {
            swal({
                title:"No hay Fecha seleccionada.",
                text:'',
                type:'warning',
                showCancelButton:false,
                confirmButtonClass:'btn-warning',
                confirmButtonText:'Corregir',
            });
        }else{
            $.ajax({
                method:'POST',
                headers:{'X-CSRF-TOKEN':token},
                url:'http://201.236.212.130:82/intranetcercafe/public/admin/report_destete_finalizacion_conversion_final_granjas',
                data:json
            }).done(function (msg){
                console.log(msg);
                console.log(array_granjas);
                array_granjas.length=0;
            })
        }
    })

the array farms what makes me clean the array but not the select

    
asked by Juan Esteban Yarce 09.03.2018 в 19:56
source

1 answer

1

To me of the impression that you want it to work so that

      var array_granjas = [];
    $('#select_granjas').on('change', function(e){
        array_granjas.push($(e.currentTarget).val())
        console.log('value_select_prueba: --------->', array_granjas);
       $(this).val("");
    });
        
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group">
    <select name="" id="select_granjas" class="form-control">
      <option value="" selected="selected">Seleccione Granja: </option>
      <option value="1">Granja 1</option>
      <option value="2">Granja 2</option>
      <option value="3">Granja 3</option>

    </select>
  </div>
Try adapt it to your project. The only difference is this line $(this).val(""); that you enter in the change of the select. What that line does is that the value of the select is "" , then it goes to the value of the select that has that in this case the first option. Hope this can help you.     
answered by 09.03.2018 в 20:19