To enable and disable a text type input after a value selected in the combobox

Asked

Viewed 6,574 times

0

My jQuery

 < script >
   $(document).ready(function() {

     var tipo = $("#idTipoParticipante option:selected").text();


     if (tipo == 'Personagem') {

       $("#enable").click(function() {
         // habilitando o campo
         $('#personagem').attr("disabled", '');
         // retornando a cor padrao
         $('#personagem').css("background-color", "#FFF");

       });

     } else if (tipo == 'Tecnico') {
       // campo desabilitado
       $('#personagem').attr("disabled", true);
       // cor de fundo para o campo
       $('#personagem').css("background-color", "#cccccc");

     }

   }); < /script>

My Select

<select class="form-control" name="idTipoParticipante" id="idTipoParticipante">
  <% List<TipoParticipante>listTipoParticipante = tipoParticipanteController.retornarListaTipoParticipante(); for (int i = 0; i
    < listTipoParticipante.size(); i++) { %>
      <option value="<%=listTipoParticipante.get(i).getIdTipoParticipante()%>">
        <%=listTipoParticipante.get(i).getDescricaoTipoParticipante()%>
      </option>

      <% } %>
</select>

My input

<input type="text" class="form-control" id="personagem" name="personagem" placeholder="informe a personagem...">

2 answers

1

Do the verification in the event change in the select field:

$('#idTipoParticipante').on('change', function() {
  var tipo = $(this).find('option:selected').text().trim();
  if(tipo == 'Personagem') {
  	$('#personagem').prop("disabled", false);
  }
  else if(tipo == 'Tecnico') {
  	$('#personagem').prop("disabled", true);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" class="form-control" id="personagem" name="personagem" placeholder="informe a personagem...">
<select id="idTipoParticipante">
  <option></option>
  <option value="">
    Personagem
  </option>
  <option value="">
    Tecnico
  </option>  
</select>

EXAMPLE in jsfiddle

  • Miguel, fantâstico! show of balls... worked perfectly. I need to study more Javascript or jQuery. are very interesting technologies in development.

  • good thing it helped @Toothless. Yap for browser-based apps (running in the browser), is the best

1


Toothless,

a simpler example of what you published would be:

<select class="form-control" name="idTipoParticipante" id="idTipoParticipante">
  <option value="0">Personagem</option>
  <option value="1">Tecnico</option>
</select>

<input type="text" class="form-control" id="personagem" name="personagem" placeholder="informe a personagem...">

Javascript:

$(document).ready(function() {
    $('#idTipoParticipante').change(function(){
        var tipo = $("#idTipoParticipante option:selected").text();
        if (tipo == 'Personagem') {
            $('#personagem').attr('disabled', false);
        }else if (tipo == 'Tecnico') {
            $('#personagem').attr('disabled', true);
        }
    });
});

Here it is Jsfiddle.

Basically what you should do beyond the point you once were: add an action to the $('idTipoParticipante'). So every time your idTipoParticipant is modified you can compare the values again.

  • Okay, I’ll implement it now, buddy!

  • Rubico, fantâstico! show of balls... worked perfectly. I need to study more Javascript or jQuery. are very interesting technologies in development.

Browser other questions tagged

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