Return filled checkbox

Asked

Viewed 388 times

0

I have a script that returns me from the ID, the name and the checkbox Inactive, but even when someone marked as inactive my function does not return this field filled.

<script type='text/javascript'>
    $(document).ready(function(){
            $("input[name='id']").blur(function(){
                    var $nome = $("input[name='nome']");
                    var $inativo = $("input:checkbox[name='inativo']:checked");

                    $.getJSON('function_pes.php',{ 
                            id: $( this ).val() 
                    },function( json ){
                            $nome.val( json.nome );
                            $inativo.val( json.inativo );

                    });
            });
    });
</script>
<!-- Fim do script de preenchimento automático dos campos a partir do campo ID -->

               <form action="salvar_pessoa.php" method="post"><!-- Inicio Form -->
                    <div class="col-lg-2"><!-- Inicio Input ID -->
                        <label for="ex1">ID:</label>
                        <input type="text" class="form-control" maxlength="13"  name="id">
        </div><!-- Fim Input ID -->

        <div class="col-lg-5"><!-- Inicio Input Nome -->
                        <label for="ex1">Nome:</label>
                        <input type="text" required class="form-control" maxlength="50"  name="nome">
        </div><!-- Fim Input Nome -->

        <div class="col-lg-10"><!-- Inicio checkbox Registro Inativo-->
                        <div class="checkbox" style="margin-top:30px;">
                            <label>
                                <input type="checkbox" name="inativo" value="true" style="outline:none;">Registro Inativo
                            </label>
                        </div>

2 answers

1


I managed to solve the problem already, so I will post here if someone also goes through it, just change the json to receive only values that really forão marked / checkados. And so I always return only the checkbox that are marked in the database with boolean value 1

<script type='text/javascript'>
$(document).ready(function(){
        $("input[name='id']").blur(function(){
                var $nome = $("input[name='nome']");
                var $inativo = $("input:checkbox[name='inativo']:checked");

                $.getJSON('function_pes.php',{ 
                        id: $( this ).val() 
                },function( json ){
                        $nome.val( json.nome );
                        $inativo.prop('checked', !!+json.inativo );
                });
        });
});

           <form action="salvar_pessoa.php" method="post"><!-- Inicio Form -->
                <div class="col-lg-2"><!-- Inicio Input ID -->
                    <label for="ex1">ID:</label>
                    <input type="text" class="form-control" maxlength="13"  name="id">
    </div><!-- Fim Input ID -->

    <div class="col-lg-5"><!-- Inicio Input Nome -->
                    <label for="ex1">Nome:</label>
                    <input type="text" required class="form-control" maxlength="50"  name="nome">
    </div><!-- Fim Input Nome -->

    <div class="col-lg-10"><!-- Inicio checkbox Registro Inativo-->
                    <div class="checkbox" style="margin-top:30px;">
                        <label>
                            <input type="checkbox" name="inativo" value="true" style="outline:none;">Registro Inativo
                        </label>
                    </div>

0

To change the status of a checkbox, use $inativo.prop('checked', json.inativo);.
The value of a checkbox does not refer to its state, it only indicates what will be passed to the server if it is checked.

  • now returns all checkbox filled even if it has not been checked when registering

  • Check your json if it’s not always dialing true or maybe the false may be encoded in string https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean

  • already solved the problem, thanks stayed this way the json => $inactive.prop('checked', !!+json.inactive); and this way I can return all my checkbox filled and differentiate from the ones that are not filled

Browser other questions tagged

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