Selected Combox na javascript

Asked

Viewed 63 times

1

Good evening guys, I need a little help, it’s simple but I’m in a jam. I have this combo inside a MODAL, it occurs that the fields of this modal I populate everything by javascript/Json searching the data of a database.

MODAL

<!-- Grid column -->
<div class="col-lg-4 col-md-4">                                     
   <div class="md-form form-sm">
      <select class="mdb-select colorful-select dropdown-primary" searchable="Busca por nível.." id="frmnivelacesso">
         <option value="1"> Master</option>
         <option value="2"> Secretária/Recepcionista</option>
         <option value="3"> Cirurgião Dentista</option>
      </select>
      <label for="form8" class="">Nivel de acesso</label>
   </div>
</div>
<!-- Grid column -->

JAVASCRIPT

$("#ModalUsuarioEdit").on('shown.bs.modal', function(){

        var id = $('#ModalUsuarioEdit').data('id'); 
        $.post('estrutura/usuario.php',{acao:'select',id:id},function(r) { 
               var ev = jQuery.parseJSON(r);           
               if (ev.success) {


                    document.getElementById('frm-imgname').src = 'arquivos/images/' + ev.foto;
                    var nomecompleto = ev.nome + ' ' + ev.sobrenome;
                    $("#frm-username").text(nomecompleto);

                    $("#frmnome").val(ev.nome);  
                    $("#frmsobrenome").val(ev.sobrenome); 
                    $("#frmdtnascimento").val(ev.nascimento); 
                    $("#frmcargo").val(ev.cargo); 
                    $("#frmemail").val(ev.email); 
                    $("#frmtelefone").val(ev.telefone); 
                    $("#frmcro").val(ev.cro); 
                    $("#frmusuario").val(ev.usuario); 
                    $("#frmsenha").val(ev.senha); 

                    $("#frmnivelacesso").val(ev.nivel); // nesse ponto não sei como fazer com que a option da combo fique seleciona 

                    $("#frmnome").focus();
                    $("#frmsobrenome").focus();
                    $("#frmdtnascimento").focus();
                    $("#frmcargo").focus();
                    $("#frmemail").focus();
                    $("#frmtelefone").focus();
                    $("#frmcro").focus();
                    $("#frmusuario").focus();
                    $("#frmsenha").focus();
                    $("#btnfechar").focus();

               }         
        });
  • It was unclear what your doubt was.

  • Yeah, but what’s the doubt?

  • Sorry, I forgot to inform, as return from base I want to leave the parameter "Selected" active to the corresponding value. ex: Ev.nivel = 1 then <option value="1" Selected> Master</option>.

1 answer

1

You select the option for value (.val()), selecting the select for id, in this way:

$("#frmnivelacesso").val(ev.nivel);

Example:

var ev = { nivel: 2 }
$("#frmnivelacesso").val(ev.nivel);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Grid column -->
<div class="col-lg-4 col-md-4">                                     
   <div class="md-form form-sm">
      <select class="mdb-select colorful-select dropdown-primary" searchable="Busca por nível.." id="frmnivelacesso">
         <option value="1"> Master</option>
         <option value="2"> Secretária/Recepcionista</option>
         <option value="3"> Cirurgião Dentista</option>
      </select>
      <label for="form8" class="">Nivel de acesso</label>
   </div>
</div>
<!-- Grid column -->

Or using the method .prop():

var ev = { nivel: 3 }
$("#frmnivelacesso option[value='"+ev.nivel+"']").prop("selected", true);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- Grid column -->
<div class="col-lg-4 col-md-4">                                     
   <div class="md-form form-sm">
      <select class="mdb-select colorful-select dropdown-primary" searchable="Busca por nível.." id="frmnivelacesso">
         <option value="1"> Master</option>
         <option value="2"> Secretária/Recepcionista</option>
         <option value="3"> Cirurgião Dentista</option>
      </select>
      <label for="form8" class="">Nivel de acesso</label>
   </div>
</div>
<!-- Grid column -->

  • That’s what I did, but on the combo screen did not leave the selected value, look in my code in the question, this way, the value is being passed to the combo, but need that this value is selected.

  • I added another method to the answer. If neither of them works (as I said, the first one didn’t work), then we have to see what can be, because it is not normal, it should work

  • It does not work, I think it should be something in the modal, I will review, I left fixed the value for testing and did not roll.. var test = 2; $("#frm34 option[value='"+test+"']"). prop("Selected", true);

  • Only select does not work? Are the other fields receiving value? I am testing here on the server.

  • Take a look here, it works normal: http://dvdteste.housetip.ws/teste.php

  • If the code $("#frmnome").val(ev.nome);, for example, it’s working, so the $("#frmnivelacesso").val(ev.nivel); tb should work, since both are in the same block.

Show 1 more comment

Browser other questions tagged

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