Javascript Can';t在onClick函数中将变量作为参数传递

Javascript Can';t在onClick函数中将变量作为参数传递,javascript,escaping,Javascript,Escaping,我想要一个表,它的单元格中填充了来自MySQL的数据 表中有许多具有Id的TDs 我想将单元格的id传递给函数,以便编辑其内容: document.getElementById(IndexedActionButton).innerHTML = '<input type="Button" name = "EditActionButton" id="EditActionButton" onClick="SaveUpdateToActionList(&quot;Cell_ID&q

我想要一个表,它的单元格中填充了来自MySQL的数据

表中有许多具有Id的TDs

我想将单元格的id传递给函数,以便编辑其内容:

document.getElementById(IndexedActionButton).innerHTML = '<input type="Button" name = "EditActionButton" id="EditActionButton" onClick="SaveUpdateToActionList(&quot;Cell_ID&quot;)" value="Edit Action" />';

function SaveUpdateToActionList(Cell) {
    alert(Cell);   
    document.getElementById(Cell).innerHTML = 'Here'; 
}
document.getElementById(IndexedActionButton).innerHTML='';
函数SaveUpdateToActionList(单元格){
警报(单元);
document.getElementById(Cell.innerHTML='Here';
}
当我有
警报(单元格)时显示,我看到它发送文本“Cell_ID”,而我想看到那里的数据操作按钮386


非常感谢您的帮助。

您可以通过将onClick脚本设置为
SaveUpdateToActionList(此选项)来传入正在单击的元素

然后,在
SaveUpdateToActionList
的主体中,
单元格将引用刚刚单击的按钮。然后,您可以沿着DOM走到它所属的TD。

您可以尝试以下方法:

   document.getElementById(IndexedActionButton).innerHTML = '<input type="Button" name =      "EditActionButton" id="EditActionButton" onClick="SaveUpdateToActionList()" value="Edit Action" />';

   function SaveUpdateToActionList(event) {
    alert(event.target.id);   
    document.getElementById(Cell).innerHTML = 'Here'; 
   }
document.getElementById(IndexedActionButton).innerHTML='';
函数SaveUpdateToActionList(事件){
警报(event.target.id);
document.getElementById(Cell.innerHTML='Here';
}

但是,如果我正确理解您正在尝试执行的操作,您应该有一个单一的事件列表器,它可以捕获所有单元格中的bubling事件,而不是在每个单元格中附加一个onclick…

只需单独注册事件侦听器,而不必指定
innerHTML
属性:

Document.getElementById(IndexedActionButton).addEventListener('onclick', SaveUpdateToActionList);

将使用事件对象作为第一个参数调用
SaveUpdateToActionList
。您可以通过检查
event.target
属性来访问单击的元素。有关更多信息,请查看文档

输入
元素与ID为的td之间的联系是什么?它似乎完全按照您的要求执行。SaveUpdateToActionList(“Cell\u ID”)您正在传递字符串“Cell\u ID”而不是ActionButton386非常感谢!这解决了我的问题。我将研究这一点并继续前进。谢谢你的帮助。