Mark checkbox if value equals 1

Asked

Viewed 243 times

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 accuracy on the line
var permissoes = phpUnserialize(obj_permissao.permissoes); console.log(permissoes);

I get the following object:
inserir a descrição da imagem aqui

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

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

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

2 answers

3


If the idea is to check the <input> with the name corresponding to what comes in the object to 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>
<input name="aCliente" type="checkbox" >
 <input name="eCliente" type="checkbox" >
 <input name="dCliente" type="checkbox" >
 <input name="vCliente" type="checkbox" >

  • perfect Isac, I just pulled out an equal sign to work. if (permissoes[chave] == 1)

  • @Wagnerfilho, yes it is true in his example comes text instead of number. I had not even noticed

2

To mark checkbox with jQuery, you can use the .prop('checked'), and set its value as 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>
Digite 1 para marcar: <input type="text" id="txtChecar" maxlength="1"></input>
<br>
Alvo: <input id="alvo" type="checkbox" />

Browser other questions tagged

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