110690
110690

Reputation: 35

put a check on a checkbox if its values are in database

i have these checkboxes i can already get the value from it's table but i just don't know how it would end up on checking those check boxes with values that are already inserted in my database.

 function editPageLink(id){  
    $.ajax({
        type    : 'GET',
        async   : false,  
        data    : {
            key : id
        },
        url     : baseUrl+'/admin/getdiscountinfo',
        error: function(req,error){ 
                    notify('e',req.statusText);
                },
        dataType: 'json',
        cache: false,
        success : function(msg){    
            if(Object.keys(msg).length != 0){ 
                newPage();
                saveType = 'u';
                key = msg[0].key;
                $('#discountCode').val(msg[0].discountCode);
                $('#discountName').val(msg[0].discountName);
                $('#discountDesc').val(msg[0].discountDesc);
                $('#discountType').val(msg[0].discountType);
                $('#expiration').val(msg[0].expiration);
                $('#expStartDate').val(msg[0].expStartDate);
                $('#expEndDate').val(msg[0].expEndDate);
                $('#discountValue').val(msg[0].discountValue);
                $('#productId').val(msg[0].productId); if ('.chkbox2'.val() == true){
                    $('.chkbox2').check(checked); // i dunno if this is right.
                };
                $('#discounteTransaction').val(msg[0].discountTransaction);
            }
        }
    }); 

}

i tried it but it's still not working. thanks for your help! :D

EDIT : my #productId and '.chkbox2' are the same. is this part, getting the value is right ?

 $('#productId').val(msg[0].productId); if ('.chkbox2'.val() == true){
    $('.chkbox2').check(checked);
 };

Upvotes: 1

Views: 248

Answers (6)

123bbuingbbuing
123bbuingbbuing

Reputation: 47

Try adding this to your code :D

$('.chkbox2').val(msg[0].productId,':checked',true);

Upvotes: 1

110690
110690

Reputation: 35

ah! finally. i solved it i just changed

$('#productId').val(msg[0].productId); if ('.chkbox2'.val() == true){
                $('.chkbox2').check(checked); // i dunno if this is right.
            };

to

$('.chkbox2').val(msg[0].productId,':checked',true);

yehet ohorat! haha thanks for your comments i earned ideas :D

Upvotes: 1

CSK
CSK

Reputation: 777

$('#checkbox').attr('checked')

$('#checkbox').prop('checked') // true

$('#checkbox').is(':checked') // true

$('#checkbox')[0].checked // true

Upvotes: 0

Sean
Sean

Reputation: 2418

$('.chkbox2').check(checked); // i dunno if this is right.

A standard method of checking checkboxes in jQuery is to use .prop():

$('.chkbox2').prop('checked', bool);

Where if bool = true, the checkbox will be checked. (Alternatevely, if bool is false, it will be unchecked)

You may also retrieve the current checked status by simply omitting the second argument:

$('.chkbox2').prop('checked'); // Returns true or false.

Additionally, in your if condition, you have an error:

if ('.chkbox2'.val() == true){

Should be

if( $('.chkbox2').val() == true )

As you're missing the jQuery wrapper for your selector.

Upvotes: 1

Dave O'Dwyer
Dave O'Dwyer

Reputation: 1192

$('.chkbox2').prop('checked', true);

Above code will set the checkbox as checked

$('.chkbox2').prop('checked', false);

Above code will untick the checkbox

Upvotes: 1

Ashisha Nautiyal
Ashisha Nautiyal

Reputation: 1397

$('.chkbox2').prop('checked', true); 

use this to check the check box

Upvotes: 1

Related Questions