如何向函数发送附加参数?
我有这段代码,它显示来自 Web SQL 数据库的文本:
<span contenteditable="true"
onkeyup="updateRecord('+item['id']+', this)">' + item['product'] + '</span>
当我编辑文本时,它会调用 updateRecord 函数并更新值。
function updateRecord(id, textEl) {
db.transaction(function(tx) {
tx.executeSql("UPDATE products SET product = ? WHERE id = ?",
[textEl.innerHTML, id], null, onError);
});
}
不过,我正在努力践行其中的几个价值观。所以我想指定该列。如果我在函数中将列设置为 product,上面的代码就可以工作,在下面的代码中,我尝试向函数发送一个附加参数,但它不起作用。我在这里做错了什么?
<span contenteditable="true"
onkeyup="updateRecord('+item['id']+', 'product', this)">'+ item['product'] + '</span>
function updateRecord(id, column, textEl) {
db.transaction(function(tx) {
tx.executeSql("UPDATE products SET ? = ? WHERE id = ?",
[column, textEl.innerHTML, id], null, onError);
});
}
I have this code which displays text from a Web SQL database:
<span contenteditable="true"
onkeyup="updateRecord('+item['id']+', this)">' + item['product'] + '</span>
When I edit the text it calls the updateRecord function and updates the value.
function updateRecord(id, textEl) {
db.transaction(function(tx) {
tx.executeSql("UPDATE products SET product = ? WHERE id = ?",
[textEl.innerHTML, id], null, onError);
});
}
I have several of these values I'm trying to work with though. So I would like to specify the column. The above code works if I set the column to product in the function, in the following code I'm trying to send an additional parameter to the function but it's not working. What am I doing wrong here?
<span contenteditable="true"
onkeyup="updateRecord('+item['id']+', 'product', this)">'+ item['product'] + '</span>
function updateRecord(id, column, textEl) {
db.transaction(function(tx) {
tx.executeSql("UPDATE products SET ? = ? WHERE id = ?",
[column, textEl.innerHTML, id], null, onError);
});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与任何完整的 DBMS 相同,您不能参数化列名,只能参数化值。
所以你需要这样做
Same with any full-bodied DBMS, you cannot parameterize the column names, only values.
So you need to do it like this