How to change a variable value only when radio buton is checked

Asked

Viewed 37 times

-1

In the archive .php there is a certain variable.

  <?php
    $freteselecionado = "<script>document.write(checado)</script>";

So I have two input of the kind radio to receive the value.

<input type="radio" id="radio_01" name="tipo_frete" value="1">
<input type="radio" id="radio_02" name="tipo_frete" value="2">

My function using jQuery, but something is going wrong.

<script>
        $(function() {
            let checado = false;
            $( "#radio_01" ).change(function() {
            var $input = $( this );
            if ($input.is( ":checked" )) {
                checado = true;
            }}).change();

            $( "#radio_02" ).change(function() {
            var $input = $( this );
            if ($input.is( ":checked" )) {
                checado = true;
            }}).change();
        });
</script>

How to change the php variable value only when it is actually checked? What happens is that when the GIFT the value is already TRUE.

Show 2 more comments

1 answer

1


As spoken via chat, this is the code that solves your problem.

Just put "checked" on one of the radio input, so you will always be selecting one of the front, so there is no need to hide or show the button, since the front will always be selected somehow.

<input type="radio" class="radio_opcao" name="tipo_frete" value="1" checked> PAC
<input type="radio" class="radio_opcao" name="tipo_frete" value="2"> Sedex

<input id="btn_frete" type="button" value="Pagamento">

Browser other questions tagged

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