使用SetTatAttribute将多个参数传递给函数;onclick";用Javascript?

使用SetTatAttribute将多个参数传递给函数;onclick";用Javascript?,javascript,html,parameter-passing,Javascript,Html,Parameter Passing,在我的代码中,我需要执行以下操作: function myFunction() { /*more code*/ let id = 12; let name = "something"; deleteBtn.setAttribute("onclick", "otherFunction("+id+","+name+")"); } 其中 function otherFunction(id, name){ /*does o

在我的代码中,我需要执行以下操作:

function myFunction() {
        /*more code*/
        let id = 12;
        let name = "something";
        deleteBtn.setAttribute("onclick", "otherFunction("+id+","+name+")");
}
其中

function otherFunction(id, name){
        /*does other stuf*/
}
问题是,当我运行代码并加载html页面时,会出现以下错误:
SyntaxError:missing)在参数列表之后


我不知道我做错了什么。

当涉及到语法错误时,请重新检查您键入的内容

首先,我没有看到您声明deleteBtn变量

deleteBtn.setAttribute("onclick", "otherFunction("+id+","+name+")");
检查此代码,它可以工作

document.getElementById('asd').setAttribute('onclick', `otherFunction("${id}","${name}")`);

deleteBtn.onclick=()=>otherFunction(id,name)
deleteBtn.addEventListener('click',()=>otherFunction(id,name))
该代码不会有这种效果。如果您将
名称
更改为包含空格的内容,我可以看到它具有这种效果。您应该提供并使用问题编辑器的实时演示功能。从编程的角度来说,通过将字符串混合在一起生成JavaScript是一场噩梦:很难调试,并且通常存在涉及
eval
类进程的任何安全风险。不要这样做。使用
addEventListener
和一个函数而不是字符串。像这样使用
.setAttribute('onclick','otherFunction(${id},“${name}”)`)开始工作了