How can I make the chosen value in a combo go to the select?

0

function ComboAnio(){
 var meses = new Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
  var d = new Date();
  var n = d.getMonth();
  var y = d.getFullYear();
  var select = document.getElementById("selectFechaVigencia1");
  $("#selectFechaVigencia1").html("");
$("#selectFechaVigencia1").append('<option value="other">Seleccionar...</option>');
    for (var i = n; i >= n - 2; i--) {
            var opc = document.createElement("option");
            opc.text = y + "-" + meses[i];
            opc.value = i;
            select.add(opc)
            }
}
<html DOCTYPE!>
<script src="http://code.jquery.com/jquery-1.6.1.min.js"></script>
<head>
<body>
<select id="selectFechaVigencia1" onclick="ComboAnio();">
    <option value="">seleccionar...</option>
</select>
</body>
</head>
</html>

What I want is for any value that I select from the combo to be selected.

    
asked by JessússGaarcíaa 05.09.2018 в 02:05
source

1 answer

0

I recommend that you arm the select when starting, at first glance, the data is not dynamic, so I do not see much sense that they are added using the event onclick

//Función que se ejecuta cuando termina de cargar la página
$(function() 
{
  var meses = new Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
  var d = new Date();
  var n = d.getMonth();
  var y = d.getFullYear();

  for (var i = n; i >= n - 2; i--) 
  {
    //Creo una opción
    var opcion = "<option value='" + i + "'>"+ y+"-"+meses[i] +"</option>";
    //La añado con append
    $('#Fecha').append(opcion);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<select id="Fecha">
  <option>Seleccione una opción</option>
</select>

I recommend you read a little documentation from jQuery because you're including a very heavy library to your project, and you're practically not using it in the code you sample.

Greetings

    
answered by 05.09.2018 в 13:30