How to query a select that has two values?

Asked

Viewed 54 times

-2

I have a field that I type the reference code and it already selects automatic select to facilitate the life of the user who already knows the code.

I have this select with two values in a list. I would like to know how I can do a search in select whose search value is at position 1.

$('#inputTurno2').change(function () {
  var valSelecionado = $(this).val();
  var turno = JSON.parse(valSelecionado);
  $('#iputTurno1').val(turno[1])
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" class="form-control pular_inclusa" id="iputTurno1" placeholder="cod">
<select name="turno" class="form-control" id="inputTurno2">
  <option value="['', '']">---------</option>
  <option value="[1, 1]">Alvorada</option>
  <option value="[10, 2]">Federal</option>
</select>

Otherwise I can, select some value in select and it put the value I need in the input.

2 answers

0


It is worth remembering that the parse of "['', '']" can be problematic and that codigo initially will be string. Here is the documentation $().each

$('#iputTurno1').blur(function () {
  var codigo = Number($(this).val());
  $("#inputTurno2 option").each(function () {
    var rawValues = $(this).val();
    var values = JSON.parse(rawValues);
    if (values && values[1] == codigo) {
        $('#inputTurno2').val(rawValues);
        // aqui ja encontramos a única opção então vamos parar o $().each
        return false
    }
  });
});
  • Yeah, I changed the parse to [0.0], and that worked. with some addition and adjustment that you proposed in the code I was able to solve another part that was unable to handle, which was put an Alert to say that there is no code typed. Thanks so much for the help, I have little time starting in java script still learning.

0

Be able to solve farm

$('#iputTurno1').blur(function () {
            var codigo = $(this).val();
            $("#inputTurno2 option").each(function () {
                var valores = $(this).val();
                var teste = JSON.parse(valores);
                if(teste[1] == codigo){
                    $('#inputTurno2').val(valores);
                }
            });

o this code via jquery.

Browser other questions tagged

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