Return filled checkbox

0

I have a script that returns me from the Id, the name and the checkbox Idle, but even when someone is 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>
    
asked by anonymous 09.10.2017 / 22:15

2 answers

1

I've been able to solve the problem, so I'll post it here if someone goes through it, just change the json to receive only values that were actually checked / checked. And so I always return only the checkboxes 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>
    
13.10.2017 / 20:05
0

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

    
09.10.2017 / 22:50