将对象及其方法传递给 Javascript 中的 onchange 函数
Passing object and its methods to onchange function in Javascript
我正在使用 TableExport.js 脚本导出 html table,我需要在通过隐藏我不想要的行来过滤数据后导出数据. TableExport 的方法 .update()
在这里非常有用,但每次过滤数据时我都无法调用它,这与 onchange
事件相关联。以下是我的代码的相关部分:
$(function () {
var exportedTable = $("#taskListTable").tableExport();
exportedTable.update({
// some options
});
$('body').on('change', '.filter', function (exportedTable) {
// some code for filtering
tableUpdate(exportedTable);
});
})
function tableUpdate(table) {
table.update({
ignoreCSS: ["tr.tableexport-ignore", "tr.tableexport-ignore>td",
"tr.tableexport-ignore>th", "tr.hidden", "tr.hidden>td"]
});
}
我不断收到 Uncaught TypeError: Cannot read property 'update' of undefined
类型的错误。我已经阅读了很多线程(例如:Javascript passing object to function)和其他信息以了解按值传递和引用传递之间的区别,但是我阅读和尝试的次数越多,我的想法就越混乱。那么任何人都可以看到我的方法失败了什么吗? - 虽然它有一部分是遗留代码,但如果有必要,我愿意重新构建其中的大部分代码。
我想这更多是 JS 滥用的问题,所以我希望这个问题对使用 TableExport 的人和有类似 JS 问题的人都有用。所以我召唤你,JavaScript Jedis!
使用事件对象作为参数调用事件处理函数。您可能只想从范围访问 exportedTable
,而不是将其作为参数接受。
正如@guest 所说,您只需从参数中删除 exportedTable:
$('body').on('change', '.filter', function () {
# some code for filtering
tableUpdate(exportedTable);
});
我正在使用 TableExport.js 脚本导出 html table,我需要在通过隐藏我不想要的行来过滤数据后导出数据. TableExport 的方法 .update()
在这里非常有用,但每次过滤数据时我都无法调用它,这与 onchange
事件相关联。以下是我的代码的相关部分:
$(function () {
var exportedTable = $("#taskListTable").tableExport();
exportedTable.update({
// some options
});
$('body').on('change', '.filter', function (exportedTable) {
// some code for filtering
tableUpdate(exportedTable);
});
})
function tableUpdate(table) {
table.update({
ignoreCSS: ["tr.tableexport-ignore", "tr.tableexport-ignore>td",
"tr.tableexport-ignore>th", "tr.hidden", "tr.hidden>td"]
});
}
我不断收到 Uncaught TypeError: Cannot read property 'update' of undefined
类型的错误。我已经阅读了很多线程(例如:Javascript passing object to function)和其他信息以了解按值传递和引用传递之间的区别,但是我阅读和尝试的次数越多,我的想法就越混乱。那么任何人都可以看到我的方法失败了什么吗? - 虽然它有一部分是遗留代码,但如果有必要,我愿意重新构建其中的大部分代码。
我想这更多是 JS 滥用的问题,所以我希望这个问题对使用 TableExport 的人和有类似 JS 问题的人都有用。所以我召唤你,JavaScript Jedis!
使用事件对象作为参数调用事件处理函数。您可能只想从范围访问 exportedTable
,而不是将其作为参数接受。
正如@guest 所说,您只需从参数中删除 exportedTable:
$('body').on('change', '.filter', function () {
# some code for filtering
tableUpdate(exportedTable);
});