How to enable or disable select nearest?

Asked

Viewed 210 times

0

I’m using Materialize CSS, I have a screen where I would like the Switch to be activated, the select the nearest becomes active so the user can select an option, and if the Switch goes back to inactive the select also return to idle state.

HTML

 <form id="relatorio" name="relatorio" class="col s12">
 <div class="row">
   <div class="input-field col s4 l4">
     <select id="filtro2" name="filtro2" disabled>
       <option value="" disabled selected>Filtro</option>
       <option value="1">Opção 1</option>
       <option value="2">Opção 3</option>
       <option value="3">Opção 3</option>
     </select>
     <label>Filtro</label>
   </div>
   <div class="switch col s2 l2">
     <div class="switch">
       <label>
         Não
         <input type="checkbox">
         <span class="lever"></span>
         Sim
       </label>
     </div> 
   </div>

    <div class="input-field col s4 l4">
     <select id="filtro" name="filtro" disabled>
       <option value="" disabled selected>Filtro</option>
       <option value="1">Opção 1</option>
       <option value="2">Opção 3</option>
       <option value="3">Opção 3</option>
     </select>
     <label>Filtro</label>
   </div>
   <div class="switch col s2 l2">
     <div class="switch">
       <label>
         Não
         <input type="checkbox">
         <span class="lever"></span>
         Sim
       </label>
     </div> 
   </div>

 </div><!-- /row -->
</form>

JS

$(document).ready(function () {
    $(".switch").find("input[type=checkbox]").on("change",function() {

        var filtro = $(this).closest("input[type=checkbox]").prop('checked');

        if(filtro == true) {   
            alert("true");
            //$(this).closest('select').prop('disabled', true);
        } else {
            alert("false");
            //$(this).closest('select').prop('disabled', false);
        }
    });
});

I can already get the Switch value to know when to enable select and when to disable it, but I can’t find which the select nearest to change his state.

Are several selects I need to do this, so finding the one closest to the Switch and swapping out its value is essential.

Thank you in advance.

1 answer

1


You will be able to select the element using:

$(this)
.closest('.switch.col')
.prevAll('div.input-field:first')
.find('select')

Will get the div nearest that contains a select and select this select.

See working:

$(document).ready(function () {
   $(".switch").find("input[type=checkbox]").on("change",function() {

      var filtro = $(this).closest("input[type=checkbox]").prop('checked');

      var selects = $(this)
         .closest('.switch.col')
         .prevAll('div.input-field:first')
         .find('select');

      if(filtro) {   
         alert("true");
         selects.prop('disabled', true);
      } else {
         alert("false");
         selects.prop('disabled', false);
      }
   });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="relatorio" name="relatorio" class="col s12">
 <div class="row">
   <div class="input-field col s4 l4">
     <select id="filtro2" name="filtro2" disabled>
       <option value="" disabled selected>Filtro</option>
       <option value="11">Opção 1</option>
       <option value="2">Opção 3</option>
       <option value="3">Opção 3</option>
     </select>
     <label>Filtro</label>
   </div>
   <div class="switch col s2 l2">
     <div class="switch">
       <label>
         Não
         <input type="checkbox">
         <span class="lever"></span>
         Sim
       </label>
     </div> 
   </div>

    <div class="input-field col s4 l4">
     <select id="filtro" name="filtro" disabled>
       <option value="" disabled selected>Filtro</option>
       <option value="1">Opção 1</option>
       <option value="2">Opção 3</option>
       <option value="3">Opção 3</option>
     </select>
     <label>Filtro</label>
   </div>
   <div class="switch col s2 l2">
     <div class="switch">
       <label>
         Não
         <input type="checkbox">
         <span class="lever"></span>
         Sim
       </label>
     </div> 
   </div>

 </div><!-- /row -->
</form>

Tip

To compare boolean values, you don’t need to do it the way if(filtro == true). Just do it like this: if(filtro).:

if(filtro == true) -> if(filtro)
if(filtro == false) -> if(!filtro)
  • I can’t understand why my code didn’t work, now it seems to be getting the correct select, except if there are 2 in it <div class="row">, then he selects the two, and select has not activated in either case, I tried to change .prop for .atrr, but I did not succeed.

  • @Dobrychtop Never use .attr to change true or false values because it doesn’t work... Well, show me an example of how it would be with 2 selects to see how it would look.

  • I solved the problem of not selecting, this was caused by Materialize and how he makes the selects, destroyed them before switching property and booted, now I have the problem of selecting the two in the same row

  • I edited the question with 2 selects in the same row

  • @Dobrychtop modified answer.

  • It worked perfectly, thank you for your attention.

  • @Dobrychtop Dispo! Still put a tip at the end of the answer. Just update. Abs!

Show 2 more comments

Browser other questions tagged

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