disable a field from a select value

Asked

Viewed 74 times

-2

greetings! I’m trying to do the following: in my select field has several values among them the other one that should only be used if the value is not in the field. and a text field coutro which by default is disabled and is only enabled if the user clicks another. I tried this way with Jquery but without success. I already know a lot of JS and Jquery. my Jquery script

<script>
  $(document).ready(function(){
     $('#tipo_contac').on('change',function(){
      let tipo_contac = $(this).val();
       if(tipo_contac === '{{$beneficiario->id}}')
                    {
                      $('#outro_cont').removeAttr('disabled');
                    }
                    else
                    {
                        $('#outro_contac').attr('disabled',true);
                    }
                });
                    
            }); 

part of the form

 <label>Nome do Contactante</label>

<div class="input-field col s4"> 
    <select  name="tipocontactante_id" id="tipo_contac"  > 
        <option disabled="disabled" selected="selected default">Seleccione</option>
        @foreach($tipo as $beneficiario)<!-- chama os dados dinamicamente do banco-->
        <option value="{{$beneficiario->id}}">{{$beneficiario->tipo_contactante}}</option>
        @endforeach
    </select>
    <label>Tipo Contactante</label>   
</div>
<div class="input-field col s4">
    <input type="text" name="empresa_contact">
    <label>Empresa</label>
</div>

<div class="row">
    <div class="input-field col s4">
        <input type="text" name="contactante_outro" id= "outro_cont" disabled>
        <label>Outro Contactante</label>
    </div>
</div>
  • 1

    Dude, I strongly recommend you be like asking a question here in the OS. Another recommendation is you research more before asking a question. Your question is badly written and not very explanatory, help us to help you.

  • thanks for the recommendation

1 answer

1


Despite the complicated text I will try to help

$(document).ready(function(){
   $('#tipo_contac').on('change',function(){ 
     let tipo_contac = $(this).val(); 
     if(tipo_contac != 'valor esperado') {
       $('#outro_contac').attr('disabled', false);
     }else {
       $('#outro_contac').attr('disabled','disabled');
     }
   });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id=tipo_contac>
  <option value=1>oi</option>
  <option value=2>oi</option>
  <option value=3>oi</option>
  <option value='valor esperado'>hahaaaaa</option>
 </select>
 <input id=outro_contac>

php to build the option:

$num_linhas = mysqli_num_rows($resultado_query);
for($x=0;$x<$num_linhas;$x++){
  $option = mysqli_result($resultado_query,$x,"Coluna");
  echo "<option value=$option>$option</option>";
} 
  • this, but I tried to put in my expected value id=5 which corresponds to the value="other" of my type table so that it can enable the text field and still not enable. just to point out I’m bringing the table type from the bank to the form

  • just swap the string "expected value" for "other". Want help to assemble the options of select as well?

  • but I would put the "other" option manually in HTML and load the database values.

  • I appreciate the help but I’d prefer it to take even from the bank to the form. I hope there’s some way I can figure it out

  • I changed the answer to include what you wanted. Look at the end.

Browser other questions tagged

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