如何在创建表时根据值选中或取消选中复选框

时间:2015-05-06 11:04:05

标签: jquery

我根据从服务器收到的响应显示记录。

根据 appUserID 的值,我想复选框选中取消选中

appUserID有两个值1和0。

如果是1,则复选框应选中,否则复选框应取消选中

你能告诉我怎么做吗??

这是我的代码

var response = [
    {
        "phone": "2345678909",
        "appUserTypeID": "1",
        "email": "we@gmail.comj",
        "depotName": "WEQW",
        "depotID": "22",
        "appUserID": "1",
        "webEnabled": "1",
        "password": "137424",
        "appUserName": "ESSS",
        "emp_ID": "1243"
    },
    {
        "phone": "9098888888",
        "appUserTypeID": "1",
        "email": "kiran@gmail.com",
        "depotName": "Depot4",
        "depotID": "17",
        "appUserID": "0",
        "webEnabled": "1",
        "password": "783652",
        "appUserName": "Mike",
        "emp_ID": "1245"
    }
];

    var html = '';

    for(var i=0;i<response.length;i++)
    {

        var emp_id= response[i].emp_ID.trim();
        var emp_name= response[i].appUserName.trim();
        var emp_phone= response[i].phone.trim();
        var emp_email= response[i].email.trim();
        var emp_depotName= response[i].depotName.trim();
        var emp_depotID = response[i].depotID.trim();
        var appUserID = response[i].appUserID.trim();


                    html += '<tr>\
                            <td class="text-center"><input type="checkbox" checked  appUserID="'+appUserID+'"  id="'+emp_id+'"/><label for="'+emp_id+'" class="marg_none"><div></div></label></td>\n\
                            <td>'+emp_id+'</td>\n\
                            <td>'+emp_name+'</td>\n\
                            <td>'+emp_phone+'</td>\n\
                            <td>'+emp_email+'</td>\n\
                            <td>'+emp_depotName+'</td>\n\
                            <td><a data-depotid="'+emp_depotID+'" class="label label-success edit">Edit</a></td>\n\
                        </tr>';

    }

    $("#managementtablebody").html(html);

http://jsfiddle.net/vxe2d2hh/22/

1 个答案:

答案 0 :(得分:2)

根据条件添加checked属性

<td class="text-center"><input type="checkbox" ' + (appUserID == 1 ? 'checked' : '') + ' appUserID="' + appUserID + '"  id="' + emp_id + '"/><label for="' + emp_id + '" class="marg_none"><div></div></label></td>\n\

演示:Fiddle