开发者

How do I send an additional parameter to a function?

开发者 https://www.devze.com 2023-02-08 12:52 出处:网络
I have this code which displays text from a Web SQL database: <span contenteditable=\"true\" onkeyup=\"updateRecord(\'+item[\'id\']+\', this)\">\' + item[\'product\'] + \'</span>

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);
    });
  }


Same with any full-bodied DBMS, you cannot parameterize the column names, only values. So you need to do it like this

  <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 " + column + " = ? WHERE id = ?", 
          [column, textEl.innerHTML, id], null, onError);
    });
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号