Label não some Django

Asked

Viewed 32 times

1

Use a radio and a Javascript script to make the action disappear the agency field of the report Model, the problem is that add only the combobox the label does not add up.

Model

class Relatorio(models.Model):
    data_inicial = models.DateField(default=timezone.now)
    data_final = models.DateField(default=timezone.now)
    tipo = models.CharField(max_length=30, choices = TIPOS, default='ENVIO')
    usuario = models.ForeignKey(User, on_delete=models.CASCADE)
    agencia = models.ForeignKey(Group, on_delete=models.CASCADE, null=True, blank=True)

Javascript

<script>
jQuery(document).ready(function($) {
    $('input[type="radio"]').change(function(){    
        if ($(this).is('#id_tipo_1')){
             $('#id_agencia, label[for="#id_agencia"]').parent().hide()     

         }else{
             $('#id_agencia, [for="#id_agencia"]').parent().show()

         }
   });
});
</script>

Demonstration:

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

1 answer

1


Your mistake is that you are placing the symbol # label selector, and this way the selector does not find the element:

[for="#id_agencia"]
      ↑

When it should just be:

[for="id_agencia"]

You don’t have to include label on the dial, just like this:

jQuery(document).ready(function($) {
   $('input[type="radio"]').change(function(){    
      if ($(this).is('#id_tipo_1')){
         $('#id_agencia, [for="id_agencia"]').parent().hide()     
      }else{
         $('#id_agencia, [for="id_agencia"]').parent().show()
      }
   });
});

An example:

jQuery(document).ready(function($) {
   $('input[type="radio"]').change(function(){    
      if ($(this).is('#id_tipo_1')){
         $('#id_agencia, [for="id_agencia"]').parent().hide()     
      }else{
         $('#id_agencia, [for="id_agencia"]').parent().show()
      }
   });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="radio" name="rad" id="id_tipo_2" checked>Envio
<input type="radio" name="rad" id="id_tipo_1">Recebimento
<br>
<table>
   <tr>
      <td>
         <label for="id_agencia">Agência</label>
      </td>
      <td>
         <select id="id_agencia">
            <option>------</option>
         </select>
      </td>
   </tr>
</table>

Browser other questions tagged

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