visible or invisible input when selecting checkbox

Asked

Viewed 391 times

1

The Form has a checkbox that when selecting shows an input that is hidden.

It is working correctly, when I enter the page the input is hidden and is only visible when I select the checkbox, as I show:

<div class="table-responsive">  
<label for="IniciarTarefa">Tarefa a Par</label>
<input type="checkbox" name="check" id="check" value="Sim"/>
<div class="form-group input-group input-group-lg">
    <span class="input-group-addon">
    <span class="glyphicon glyphicon-user"></span>
    </span>
    <select class="form-control" name="Acompnhante" id="Acompnhante" style="display:none" required="" placeholder="Acesso">
      <option></option>
      <?php        
         $sql = "SELECT * FROM raddb.usuarios ORDER BY nome ASC";
         $qr = mysqli_query($conn, $sql);
         while($ln = mysqli_fetch_assoc($qr)){
            echo '<option value="'.$ln['id'].'">'.$ln['nome'].'</option>';
         }
      ?>        
    </select>
  </div>
</div> 

Script:

$("#check").click(function(){
if($(this).val()=="Sim"){
$("#Acompnhante").css("display","block");
$(this).val("false");
}
else{
$("#Acompnhante").css("display","none");
$(this).val("Sim");
}
});

The problem arises when I enter in the database and with AJAX clean this input and the checkbox within the function success, in this way:

$.ajax({
        url: './insertarefa',
        type: 'POST',
        cache: false,
        data: dadosajax,
        error: function(){

        },
        success: function(result)
        { 
        $('#check').attr('checked', false);
        $("#Acompnhante").val("");
        }
    });

After clearing the two fields with AJAX, when I select the checkbox hides the input, changes the process, because when I clean the two fields the input is visible. How can I solve?

  • 2

    Wouldn’t it be because it was missing put $("#Acompnhante").css("display","none"); within the success: { } ?

  • @hugocsl with this line of code when cleaning puts the input back hidden, but even exchange the process, only opens when I withdraw the selection

2 answers

1


Change how to hide/show the widget based on whether the checkbox is checked:

$("#check").click(function(){

   $("#Acompnhante")[this.checked ? "show" : "hide"]();

   if($(this).val()=="Sim"){
      $(this).val("false");
   }
   else{
      $(this).val("Sim");
   }
});

See: if the element is checked, call the function show() (show), if not, call the function .hide() (hide). For this I used a ternary operator alternating between the two functions:

And in the success, add .hide() to the element:

$("#Acompnhante").val("").hide();
                           ↑
                   esconde o elemento

And return the value to "Yes" at the checkbox:

$('#check').prop('checked', false).val("Sim");
                                    ↑

Example:

function simular(){
   $("#Acompnhante").val("").hide();
   $('#check').prop('checked', false).val("Sim");
}


$("#check").click(function(){

   $("#Acompnhante")[this.checked ? "show" : "hide"]();

   if($(this).val()=="Sim"){
      $(this).val("false");
   }
   else{
      $(this).val("Sim");
   }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="table-responsive">  
<label for="IniciarTarefa">Tarefa a Par</label>
<input type="checkbox" name="check" id="check" value="Sim"/>
<div class="form-group input-group input-group-lg">
    <span class="input-group-addon">
    <span class="glyphicon glyphicon-user"></span>
    </span>
    <select class="form-control" name="Acompnhante" id="Acompnhante" style="display:none" required="" placeholder="Acesso">
      <option></option>
      <option value="1">1</option>
      <option value="2">2</option>
    </select>
  </div>
</div>

<button onclick="simular()">Simular Ajax</button>

  • only one question. When I select the checkbox and fill in select, sometimes you enter yes and some times you enter false, when I do not fill you enter empty. But when I select the checkbox and select it, I should always enter yes.

  • I solved it this way, so I always get the same value, I traded this if($(this).val()=="Sim"){&#xA; $(this).val("false"); for if($(this).val()=="Sim"){&#xA; $(this).val("Sim");, and it works well, but I don’t understand why it’s happening

  • That’s right. I already changed the answer. Failed to restore checkbox value.

0

This is because you check if the value of #Escort is 'yes', if it is anything else it is invisible. You can perform a checkbox event by javascript instead of setting its attribute and #Escort value. But I believe that change in your Success $("#Acompnhante").val(""); for $("#Acompnhante").val("Sim"); should already solve;

  • tested the way you suggested, but the result is the same, after cleaning, change the process, because the problem is that the page does not update or can update

Browser other questions tagged

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