javascript - DataTables function- table.row.add ,不使用表格内的按钮(javascript)
问题描述
我正在使用 DataTables 库创建带有“下载”按钮的表。
在第一行,按钮正在工作,但在其余行不起作用(我正在使用循环将数据输入到表中)。
我究竟做错了什么?
JS代码:
snapshot.forEach(function(childSnapshot) {
var childData = childSnapshot.val();
number = childData.Number;
table.row.add( [
number,
"<button id='script'>Download Files</button>"
] ).draw( false );
button = document.getElementById('script');
button.onclick = function(){ myScript(number)};
});
解决方案
您创建了许多具有相同 id 的按钮,因此document.getElementById('script');
将始终返回具有此 id 的相同第一个元素。
你可以尝试这样的事情:
snapshot.forEach(function(childSnapshot, i) {
var childData = childSnapshot.val();
number = childData.Number;
table.row.add( [
number,
`<button id='script${i}'>Download Files</button>`
] ).draw( false );
button = document.getElementById(`script${i}`);
button.onclick = function(){ myScript(number)};
});
推荐阅读
- html - 如何打印带有边界的动态内容的页面而不破坏它?
- ios - 下载进度应该显示整个应用程序 IOS Swift 吗?
- javascript - 无法解决 Nodejs Express 中的承诺
- python - Errno2 没有使用 np.save 的此类文件或目录
- gradle - 运行 gradlew bootRun 命令时出现 Gradle 命令错误
- python - Python 3.7递归删除动态JSON字典/列表中的项目名称不在列表中
- javascript - 如何在javascript中的警报中放置一个id
- reactjs - 如何将 scss 文件作为道具传递给反应组件?
- kubernetes - 从私有注册表中提取时自动使用秘密
- coldfusion - cflock 或不锁定的问题