Form with year loop manufacture

Asked

Viewed 35 times

0

Good afternoon! I am developing a form for registration of cars, where I will inform the year of manufacture and year that stopped manufacturing. I am pulling the dice a table to select the manufacturing year, and for the final year I am recovering the value with the following code:

 <script>
   $('#ano').change(function(e) {
   var texto = document.getElementById('ano').value;
   document.getElementById('ano2').value = texto;
   })
 </script>

I need to take this amount and add up until it is the final year of the vehicle and make this value appear in my select form.

                       <div class="field"> 
                            <label class="label">Ano do Veículo Final</label>
                            <div class="select is-fullwidth">
                                <div class="field">
                                    <select name="ano2" id='ano2'> 
                                       
                                    </select>       
                                </div>
                            </div>                                    
                        </div>

1 answer

0

Pass the variable value texto for a function that will popular select ano2

In the function that will popular, perform a loop do .. while that will add the options

       var anoFabricado=2010; // puxado de uma tabela 
       
    $('#ano').change(function(e) {
       // passe esse valor para uma função que vai popular o select ano2
       var texto = document.getElementById('ano').value;
       //opcional
       document.getElementById("ano2").innerHTML = '<option value="" disabled selected>Ano fabricação até Final</option>';
       popularSelect(texto);
    })
       
     function popularSelect(anoFinal){

        //elemento a ser populado
        var meuSelect = document.getElementById("ano2");
  
        var LeandroDias = function(max){
        
            do{
              meuSelect.add(new Option(anoFabricado++,max--),null);
            }while(max>=0);
            
         }(anoFinal-anoFabricado);
    };
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
                               
    <select name="ano" id='ano'> 
      <option>2011
      <option>2012
      <option>2013
      <option>2014
      <option>2015
      <option>2016
      <option>2017
      <option>2018
      <option>2019
      <option>2020
    </select>

    <select name="ano2" id='ano2'> 
    </select>

Browser other questions tagged

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