1
I have a radio, which changes the input according to the selected radio, but I would like it to be selected as soon as I load the page according to the value of the Session I pass to the html page. I’m changing the radios and making Trigger change but it doesn’t work, follow the code below.
HTML
<div class="divRadio">
<input type="radio" name="porcentagemRadio" id="1" class="pg porcentagemRadio" value="1"/> Porcentagem
<input type="radio" name="porcentagemRadio" id="2" class="pg porcentagemRadio" value="2"/> Valor absoluto
</div>
<div class="divPorcentagem" id="porcentagem2">
<label class="control-label porcentagem" for="porcentagem">Porcentagem: </label>
<select name="porcentagem" id="porcentagemSelect" class="form-control porcent">
<option value="">Selecione a Porcentagem</option>
<option value="20">20%</option>
<option value="10">10%</option>
<option value="15">15%</option>
<option value="30">30%</option>
<option value="35">35%</option>
<option value="40">40%</option>
<option value="45">45%</option>
<option value="50">50%</option>
</select>
</div>
<div id="precoAbsoluto" class="divAbsoluto">
<label for="preco2" class="control-label">Preço: </label>
<input type="number" name="preco2" id="preco1" class="form-control absoluto">
</div>
<div id="precoAbsoluto2" class="divAbsoluto2">
<label for="preco" class="control-label">Preço: </label>
<input type="number" name="preco" id="preco" readonly="true" class="form-control absoluto2">
</div>
JS
$(document).ready(function () {
$("#porcentagem2").hide();
$("#precoAbsoluto").hide();
$("#precoAbsoluto2").hide();
$('.porcentagemRadio').on('change',function () {
$("#preco1").val("");
$("#preco").val("");
if($(this).val() === '1'){
$("#porcentagem2").show();
$("#precoAbsoluto").hide();
$("#precoAbsoluto2").show();
}
if($(this).val() === '2'){
$("#porcentagem2").hide();
$("#precoAbsoluto").show();
$("#precoAbsoluto2").
document.getElementById(<?php echo $_SESSION['porcentagemRadio']; ?>).checked = true;
$(".porcentagemRadio").trigger("change");
Thanks for the help, it really worked and I’m sorry for the mistakes, as I did not send whole code I ended up sending without some parts unintentionally, but thank you!
– LMT