omalukinho
omalukinho

Reputation: 33

how Mark Checkboxes after create a dynamic form in PHP using JQuery,

I am trying to check some checkboxes based on it value. In my code, I can update the database using this ckbxs, but when I try load the updated date, the status is showed as uncheck. So, I am trying to read the html, after create the form, to check the values and then check the ckbx. What can I do here to fix it? Thanks in advance

$(document).ready(function(){
    $('input[type=checkbox]').each(function () {
       if (this.value == 1) {
            (function(){this.attr('checked', true);})
        }
    } )
})

Upvotes: 0

Views: 181

Answers (3)

omalukinho
omalukinho

Reputation: 33

Thanks Willian, now I am using a onChange and I got what I need...

function changerCB(idCheck){

$(document).ready(function() {
$.post('ajaxUpdate.php', {
    id: document.getElementById(idCheck).id,
    value: document.getElementById(idCheck).checked?1:0
    }, function(data){
        $('#display').html(data.returnValue)
        if(data.success) {              
    } else {
    }
}, 'json');
return false;  
}); 
} 

Upvotes: 0

kojiro
kojiro

Reputation: 77157

$(document).ready(function () {
    $('input[type="checkbox"][value="1"]').each(function () {
        $(this).attr('checked', true);
    });
});

Upvotes: 0

Will
Will

Reputation: 20235

You need to enclose the thiss in a $( ) and change value to val().

$(document).ready(function(){
    $('input[type=checkbox]').each(function () {
       if ($(this).val() == 1) {
            $(this).attr('checked', true);
       }
    });
});

Upvotes: 1

Related Questions