Check checkbox if value is equal to 1

0

Hello! Through the function below:

        success: function( tbl_permissao ) {
            obj_permissao = tbl_permissao;

            var permissoes = phpUnserialize(obj_permissao.permissoes);
            console.log(permissoes);

            $.each(permissoes, function (name, valor) {                 
                var $el = $('[name="' + name + '"]'),
                type = $el.attr('type');
                switch (type) {
                    case 'checkbox':                            
                     $el.prop('checked', true);                         
                     break;
                    case 'radio':
                     $el.filter('[value="' + valor + '"]').attr('checked', 'checked');
                     break;
                    default:
                    $el.valor(valor);
                }
            });

            obj_form();
            ret = true           
            $('#modal_permissao').modal('show');
            $('.modal-title').html('<i class="fa fa-lock "></i> Editar Permissão');
        },

More accuracies in the line% var permissoes = phpUnserialize(obj_permissao.permissoes); console.log(permissoes);

I get the following object:

I now need Check / Check elements of type checkbox, whose valor is equal to 1 .

Using the function below, all elements of the checkbox type are being marked, so can you help me with this and only check if value equals 1?

<input name="aCliente" type="checkbox" > <input name="eCliente" type="checkbox" > <input name="dCliente" type="checkbox" > <input name="vCliente" type="checkbox" >

    
asked by anonymous 21.07.2017 / 18:34

2 answers

3

If the idea is to check the <input> with the name corresponding to what comes in the object the 1 can do the following:

var permissoes ={ 
  aCliente : null,
  eCliente: 1,
  dCliente: 1,
  vCliente: null
};

for (var chave in permissoes){ //percorrer todos os campos do objeto
  if (permissoes[chave] == 1){ //ver se o campo vem a 1
    $("input[name=" + chave +"]").prop('checked', true); //por checked através de prop
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputname="aCliente" type="checkbox" >
 <input name="eCliente" type="checkbox" >
 <input name="dCliente" type="checkbox" >
 <input name="vCliente" type="checkbox" >
    
21.07.2017 / 18:57
2

To mark checkbox with jQuery, you can use .prop('checked') , and set its value to true or false

$(document).delegate('#txtChecar','keyup',function (e) {
   //Verifica se o valor digitado foi igual a 1
   var deve_marcar = ($(this).val() == "1") ? true : false;
   $('#alvo').prop('checked', deve_marcar);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>Digite1paramarcar:<inputtype="text" id="txtChecar" maxlength="1"></input>
<br>
Alvo: <input id="alvo" type="checkbox" />
    
21.07.2017 / 18:51