jquery - DataTables - 带有“onchange”的复选框不起作用
问题描述
我正在使用 dataTables 并希望包含一个复选框,该复选框将在更改时更新我的数据(选择、取消选择)。数据显示在表格中,每行都有复选框;然而:
- this.checked 无法识别(返回“未定义”);和
- 当我选择或取消选择复选框时,控制台日志中出现错误:Uncaught SyntaxError: Unexpected token ','
代码是:
{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>
推荐阅读
- wordpress - 在 PubSubHubbub 订阅 Wordpress 博客
- c - 为什么我的冒泡排序出现分段错误?
- react-native - 转到 React-Native Flatlist 中的详细信息页面
- travis-ci - 同一阶段的 Travis CI 测试是否发生在同一实例中?
- java - 使用 TestnG 运行 Selenium 时,配置失败:BeforeClass 设置?
- autodesk-forge - 更改查看器中的光标图标
- python - 调整图像大小 Python Tkinter
- docker - 从主机启动 docker-compose 中的 cron 作业?
- node.js - React-Bootstrap-Table-Next 表中只有一行数据
- java - 可执行 Jar 不能在命令行中工作,但在 Eclipse 中工作正常