Checkbox checked属性在JQUERY Datatable列中不起作用

时间:2016-04-11 10:10:53

标签: javascript c# jquery asp.net-mvc-4

我有一个jquery datatable,其中第一个颜色为checkboxes。在编辑时,我需要将所选项目显示为checkbox 选中我从ajax response获取数据。数据绑定工作正常,但checked属性不正常。

这是我的代码:

custAct.success = function (status) {
       try {
               var response = JSON.parse(status);
               activityCust.clear().draw();

  for (var i = 0; i < response.length - 1 ; i++) 
      {
activityCust.row.add({ 0: '<input class="checkboxCust" checked=' + response[i].IsSelected + '  value="' + response[i].ID + '" data-id="' + response[i].ID + '" type="checkbox" />', 1: response[i].Name, 2: response[i].Hours }).draw();    
      }

catch(ex)
{
}

Html代码:

<table class="gridTableActivityCust hover">
                            <thead>
                                <tr>
                                    <th>Select</th>
                                    <th>Activity Name</th>
                                    <th>Hours</th>
                                </tr>
                            </thead>
                            <tbody>
                                @foreach (var item in Model.Activities)
                                {
                                    <tr data-id="@item.ActId">
                                        <td>
                                            <input class='checkboxCust' disabled="@item.Enabled" checked="@item.IsSelected" value="@item.ActId" data-id="@item.ActId" type='checkbox' />
                                        </td>
                                        <td>
                                            @item.ActivityName
                                        </td>
                                        <td>
                                            @item.Hours
                                        </td>
                                    </tr>
                                }
                            </tbody>
                        </table>

这是json回复:

[
  {
    "ID": 1,
    "Name": "Activity 1",
    "Description": "nc",
    "Hours": 2,
    "Remove": false,
    "Active": false,
    "IsSelected": false,
    "Disabled": true
  },
  {
    "ID": 2,
    "Name": "Activity 2",
    "Description": "lkn",
    "Hours": 3,
    "Remove": false,
    "Active": true,
    "IsSelected": false,
    "Disabled": false
  },
  {
    "ID": 3,
    "Name": "Activity 3",
    "Description": "lkk",
    "Hours": 2,
    "Remove": false,
    "Active": false,
    "IsSelected": false,
    "Disabled": true
  },
  {
    "ID": 4,
    "Name": "Activity 4",
    "Description": "kjhj",
    "Hours": 3,
    "Remove": false,
    "Active": true,
    "IsSelected": false,
    "Disabled": false
  },
  {
    "ID": 5,
    "Name": "Activity 5",
    "Description": "mn",
    "Hours": 7,
    "Remove": false,
    "Active": true,
    "IsSelected": true,
    "Disabled": false
  },
  {
    "ID": 6,
    "Name": "Activity 6",
    "Description": "kj",
    "Hours": 5,
    "Remove": false,
    "Active": true,
    "IsSelected": false,
    "Disabled": false
  },
  {
    "ID": 7,
    "Name": "Activity 7",
    "Description": "hj",
    "Hours": 8,
    "Remove": false,
    "Active": false,
    "IsSelected": false,
    "Disabled": true
  },
  {
    "ID": 8,
    "Name": "gdfg",
    "Description": "dgdfg",
    "Hours": 4.4,
    "Remove": false,
    "Active": true,
    "IsSelected": false,
    "Disabled": false
  }
]

在回复中仅选择了活动5 。但是在UI中,所有复选框都被选中。

截图:

img

在这种情况下,只应检查活动5。

0 个答案:

没有答案