0
I am with a form with radio Buttons, and when I select the radio Buttons it will appear/ hiding the fields I want.
So far so great, but if I select an option on and then change radio button, that field disappears, but it doesn’t clear the chosen value on it, then if I send the form, the value is passed, even if the field is hidden.
Would anyone know how to clear the selected ?
I’ve tried with .empty()
, but then he erases all the values of .
form:
<div class="form-group" id="gr-003">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Vencimento da 1ª parcela <small><b>(dia)</b></small> <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 value="" id="vazio"></option>
<?php
$b = array(5,15); // ######## DIAS PARA PAGAMENTO
foreach($b as $key => $value){
?>
<option value="<?php echo $value;?>" <?php if($value == $vvencimento){ echo ' selected="selected"'; } ?> ><?php echo $value;?></option>
<?php } ?>
</select>
</div>
And the jQuery:
if (this.checked && this.value == '3') {
$("#gr-004").show();
$("#gr-002").hide();
$("#gr-003").hide();
$("#select-custom-field3").empty().append("#gr-003");
Thanks in advance!
#### EDITIONFollow the complete 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 ($vformapagamento == '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 ($vformapagamento == '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 ($vformapagamento == '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 ($vformapagamento == '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 ($vformapagamento == '5')?'checked':'';?> > Cheque
<br />
</div>
</div>
<div class="form-group" id="gr-002">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Qtde. Parcelas <span class="required">*</span><br><small>Caso seja pagamento à vista, selecione 1</small> </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" id="gr-003">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Vencimento da 1ª parcela <small><b>(dia)</b></small> <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 value="" id="vazio"></option>
<?php
$b = array(5,15); // ######## DIAS PARA PAGAMENTO
foreach($b as $key => $value){
?>
<option value="<?php echo $value;?>" <?php if($value == $vvencimento){ echo ' selected="selected"'; } ?> ><?php echo $value;?></option>
<?php } ?>
</select>
</div>
</div>
<div class="form-group" id="gr-004">
<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 $vdatarecebimento; ?>" data-inputmask="'mask' : '99/99/9999'">
<br>
</div>
</div>
And the full jQuery:
$(document).ready(function() {
$("#gr-002").hide();
$("#gr-003").hide();
$("#gr-004").hide();
$('input:radio[name="custom_field[account][1]"]').on("change", function() {
if (this.checked && this.value == '1') {
$("#gr-002").show();
$("#gr-003").show();
$("#gr-004").hide();
} if (this.checked && this.value == '2') {
$("#gr-002").show();
$("#gr-003").show();
$("#gr-004").hide();
} if (this.checked && this.value == '3') {
$("#gr-004").show();
$("#gr-002").hide();
$("#gr-003").hide();
$("#select-custom-field3").val('');
} if (this.checked && this.value == '4') {
$("#gr-004").show();
$("#gr-002").hide();
$("#gr-003").hide();
} if (this.checked && this.value == '5') {
$("#gr-002").show();
$("#gr-003").show();
$("#gr-004").hide();
}
});
});
I gave an answer to your question but I was curious about the idea of
.append("#gr-003");
?– Sergio
I was only trying with Empty(), but I saw in a forum a guy saying to use the append, so I tried to use ^^
– Gabriel
You can show the rendered HTML and JS. So I can help you organize/simplify this code.
– Sergio
I edited the question and put the form and the full jquery
– Gabriel
Then each
custom_field[account][1]
has aform-group
that may have a select or an input, is that it? and when acustom_field[account][1]
be chosen only 1form-group
should be visible right?– Sergio
It will only be visible if the chosen radio is of value 3 or 4. Until then in hide/ appear the fields ta beauty, the problem is that if I fill a field, and after filled, hide it, then if I put to appear this field again, It will still be filled. And what I want is that when I hide the field, it erases its value
– Gabriel
I edited the answer, take a look.
– Sergio