Hide select field according to radio button

Asked

Viewed 526 times

0

I am trying to hide select fields according to the option chosen in the radio button I can hide the inputs, but the select fields I’m not getting, someone could help me?

Form:

<div class="form-group">
                    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">Forma de pagamento <span class="required">*</span>
                    </label>
                    <div class="col-md-6 col-sm-6 col-xs-12">
                      <input type="radio" name="custom_field[account][1]" id="id-custom_field-account-1-1" value="1" <?php echo ($vtipoCadastro == '1')?'checked':'';?> > Boleto Conta Um
                      <br />
                      <input type="radio" name="custom_field[account][1]" id="id-custom_field-account-1-2" value="2" <?php echo ($vtipo == '2')?'checked':'';?> > Cartão Conta Um
                      <br />
                      <input type="radio" name="custom_field[account][1]" id="id-custom_field-account-1-3" value="3" <?php echo ($vtipo == '3')?'checked':'';?> > Espécie <small><b>(R$)</b></small>
                      <br />
                      <input type="radio" name="custom_field[account][1]" id="id-custom_field-account-1-4" value="4" <?php echo ($vtipo == '4')?'checked':'';?> > Depósito/Transferência
                      <br />
                      <input type="radio" name="custom_field[account][1]" id="id-custom_field-account-1-5" value="5" <?php echo ($vtipo == '5')?'checked':'';?> > Cheque
                      <br />
                    </div>
                  </div>

                  <div class="form-group">
                    <label class="control-label col-md-3 col-sm-3 col-xs-12">Qtde. Parcelas <span class="required">*</span></small>
                    </label>
                    <div class="col-md-3 col-sm-3 col-xs-12">
                      <select class="form-control col-md-7 col-xs-12 select2_single forma" tabindex="-1" id="select-custom-field2"  name="custom_field[account][2]">
                        <option></option>
          <?php
                        for($a=1; $a<=12; $a++){
                        ?>
                            <option value="<?php echo $a;?>" <?php if($a == $vqtdeparcelas){ echo ' selected="selected"'; } ?> ><?php echo $a;?></option>
                        <?php } ?>
                      </select>
                    </div>
                  </div>


                  <div class="form-group">
                    <label class="control-label col-md-3 col-sm-3 col-xs-12">Dia Vencimento <span class="required">*</span></small>
                    </label>
                    <div class="col-md-3 col-sm-3 col-xs-12">
                      <select class="form-control col-md-7 col-xs-12 select2_single forma" tabindex="-1" id="select-custom-field3"  name="custom_field[account][3]">
                        <option></option>
          <?php
                        $b = array(5,15); //  ######## DIAS PARA PAGAMENTO
                        foreach($b as $key => $value){
                        ?>
                            <option value="<?php echo $a;?>" <?php if($a == $vvencimento){ echo ' selected="selected"'; } ?> ><?php echo $value;?></option>
                        <?php } ?>
                      </select>
                    </div>
                  </div>
                  <div class="form-group">
                    <label class="control-label col-md-3 col-sm-3 col-xs-12">Data do recebimento<span class="required">*</span>
                    </label>
                    <div class="col-md-3 col-sm-3 col-xs-12">
                      <input type="text" class="form-control col-md-7 col-xs-12 forma" name="custom_field[account][4]" id="input-custom-field4" value="<?php echo $vdatarecebimentoespecie; ?>" data-inputmask="'mask' : '99/99/9999'"><br>
                    </div>
                  </div>

script:

$(document).ready(function() {
      $('input:radio[name="custom_field[account][1]"]').on("change", function() {
          if (this.checked && this.value == '1') {
              $("#select-custom-field2, #select-custom-field3").show();
              $("#input-custom-field4").hide();
          } if (this.checked && this.value == '2') {
              $("#select-custom-field2, #select-custom-field3").show();
              $("#input-custom-field4").hide();
          } if (this.checked && this.value == '3') {
              $("#input-custom-field4").show();
              $("#select-custom-field2, #select-custom-field3").hide();
          } if (this.checked && this.value == '4') {
              $("#input-custom-field4").show();
              $("#select-custom-field2, #select-custom-field3").hide();
          } if (this.checked && this.value == '5') {
            $("#select-custom-field2, #select-custom-field3").show();
              $("#input-custom-field4").hide();
          }
      });
  });

And the CSS:

  input.form-control.forma {
    display: none;
  }

  select.form-control.forma {
    display: none;
  }
  • I tested your script and the selects are displayed when I choose Boleto Account One , Card Account One and Check and are hidden when I click Cash (R$) or Deposit/Transfer. What exactly is the mistake ?

  • The error is that the selects must disappear when selecting SPECIFY or DEPOSIT/TRANSFER. For me here, select are all the time appearing, regardless of the option selected, the only one that works here is the input (Date of receipt)

  • Here worked ok... No error occurs in your browser console ? If it occurs, post here.

1 answer

1

The problem was in select

<select class="form-control col-md-7 col-xs-12 select2_single forma" tabindex="-1" id="select-custom-field2"  name="custom_field[account][2]">

Because of the for of PHP, when I went to inspect element in browser it showed that select was in a <span> and not in a <select>. So I decided this way:

<select class="form-control col-md-7 col-xs-12 1select2_single forma" tabindex="-1" id="select-custom-field2"  name="custom_field[account][2]">

Placing the number 1 before the select2_single

Browser other questions tagged

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