数据表 - 'onchange'不起作用的复选框



我正在使用dataTables,并希望包含一个复选框,该复选框将在更改时更新我的数据(选择、取消选择(。数据显示在表格中,每行都有复选框;但是:

  1. this.checked未被识别(返回"undefined"(;以及
  2. 当我选择或取消选择复选框时,我在控制台日志中收到一个错误:Uncaught SyntaxError:意外的令牌",">

代码为:

{data: null,
className: "center",
render: function(data,type,row) {
return ("<input type='checkbox' id=" + data.cdId + " name='update' onchange='ymActivityPatrolFunction(" + data.cdId + ", " + this.checked + ")' style='zoom: 2.0;'>")
},
},

哪个给出:

您只能在函数中传递this,然后要获得其他值,您可以使用attr("id")来获得cdId值,使用checked来获得检查值,即:true/false。

演示代码 :

var data = [{
"cdId": "1gh",
"name": "Tiger Nixon",
},
{
"cdId": "2htf",
"name": "Garrett Winters"
},
];

var table = $('#example').DataTable({
data: data,
columns: [{
targets: 0,
data: "cdId",
render: function(data, type, row) {
//use only `this` and change data to `data.cdId` this is for dmo only..
return ("<input type='checkbox' id=" + data + " name='update' onchange='ymActivityPatrolFunction(this)' style='zoom: 2.0;'>")
},
className: "center"
},
{
"data": "name"
}
]
});
function ymActivityPatrolFunction(checkeds) {
//get id..and check if checked
console.log($(checkeds).attr("id"), checkeds.checked)
}
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/bs-3.3.7/dt-1.10.16/datatables.min.css" />
<script type="text/javascript" src="https://cdn.datatables.net/v/bs-3.3.7/dt-1.10.16/datatables.min.js"></script>
<table id="example" class="table table-striped table-bordered" width="100%">
<thead>
<tr>
<th>Checkbox</th>
<th>Name</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Checkbox</th>
<th>Name</th>
</tr>
</tfoot>
</table>

最新更新