0
Guys I got a problem!
I have a Radio that I receive the value of the bank
<div class="radio">
<label>
<input type="radio" name="empresa_tipo" value="J" <?php echo ($empresa->empresa_tipo == "J") ? "checked" : null; ?> class="ace" />
<span class="lbl"> Pessoa Jurídica</span>
</label>
<label>
<input type="radio" name="empresa_tipo" value="F" <?php echo ($empresa->empresa_tipo == "F") ? "checked" : null; ?> class="ace" />
<span class="lbl"> Pessoa Física</span>
</label>
</div>
however this radio, when selected executes the function in js that enables or disables some fields
follows script
$(document).ready(function() {
$('input:radio[name="empresa_tipo"]').on("change", function() {
if (this.checked && this.value == 'J') {
$("#razao_social, #CNPJ").show();
$("#data_nascimento, #CPF, #estado-civil, #sexo").hide();
} else {
$("#data_nascimento, #CPF, #estado-civil, #sexo ").show();
$("#razao_social, #CNPJ").hide();
}
});
});
however when I receive from the bank the value and pass to the radio which will receive Checked, it does not disable or enable, does not execute the function only when I click on the radio that will perform the function. how do I use the
checked
instead of
change
in function?
Observe the fields
<div class="form-group" id="sexo" >
<label class=" col-sm-3 control-label bolder blue">Sexo</label>
<div class="col-sm-9">
<div class="radio">
<label>
<input type="radio" name="empresa_sexo" id="empresa_sexo" value="M" <?php echo ($empresa->empresa_sexo == "M") ? "checked" : null; ?> class="ace" />
<span class="lbl">Masculino</span>
</label>
<label>
<input type="radio" name="empresa_sexo" id="empresa_sexo" value="F" <?php echo ($empresa->empresa_sexo == "F") ? "checked" : null; ?> class="ace" />
<span class="lbl">Feminino</span>
</label>
<label>
<input type="radio" name="empresa_sexo" id="empresa_sexo" value="O" <?php echo ($empresa->empresa_sexo == "O") ? "checked" : null; ?> class="ace" />
<span class="lbl">Outros</span>
</label>
</div>
</div>
</div>
<div class="space-4"></div>
<div class="form-group" id="CPF" >
<label class="col-sm-3 control-label no-padding-right" for="form-field-1"> CPF </label>
<div class="col-sm-9">
<input type="text" id="empresa_cpf" name="empresa_cpf" value="<?= $empresa->empresa_cpf ?>" class="col-xs-10 col-sm-5 input-mask-eyescript" />
</div>
</div>
<div class="form-group" id="CNPJ" >
<label class="col-sm-3 control-label no-padding-right" for="form-field-1"> CNPJ </label>
<div class="col-sm-9">
<input type="text" id="empresa_cnpj" name="empresa_cnpj" value="<?= $empresa->empresa_cnpj ?>" class="col-xs-10 col-sm-5 input-mask-eyescript-2" />
</div>
</div>
<div class="space-4"></div>
The intention is this when loading the page, according to what will come from the bank is PF = F or PJ = J. I did what you said, but it did not occur as expected, appeared all field, only hidden when I clicked on the radio
– Jean Prado
yes, I get it. You need to add this line to your code: $('input:radio[name="empresa_type"]'). Trigger("change"); as follows in the example. so that after loading the page, it triggers the function that will validate the disabled inputs and display in the correct way.
– Diego Andrade
edited the question by placing the fields that have to appear and hide
– Jean Prado
I’ve used your example Diego, but it didn’t work
– Jean Prado
Jean, I’m sorry, I don’t know why I can’t call change Rigger. I found an alternative solution by mixing javascript in jquery, see what you think. At least it works. Hugs, I hope I’ve helped
– Diego Andrade
That’s the way you went, thank you very much
– Jean Prado
happy to have helped, hugs
– Diego Andrade
Dear Diego, you would have an example of this, but using the Select tag??
– Jean Prado
just manipulate the selectors...where $('input:radio[name="empresa_type"]')val() and $(":radio:checked"). will be $("#id_do_select").
– Diego Andrade
did so: $(Document). ready(Function() { $('#orgao_type"]'). on("change", Function() { if (this.value == 'F') { $("#status, #municipal"). Hide(); } if (this.value == 'E') { $("#state"). show(); $("#municipal"). Hide(); } Else { $("#state, #municipal "). show(); } }); });
– Jean Prado
but I succeeded.
– Jean Prado
great, the important thing is to understand what you are doing to learn, mto bem jean!
– Diego Andrade
Thank you very much Diego!
– Jean Prado