在onclick事件中传递两个性能参数?

时间:2019-10-22 11:06:17

标签: javascript

我有此按钮,并希望在onclick事件中传递两个性能参数。
<button type="button" class="btn btn-info"onclick=updatebook(${bm[i].id},${bm[i].description})>Update</button>

尝试之后,出现此错误:

"Uncaught SyntaxError: missing ) after argument list"    

2 个答案:

答案 0 :(得分:2)

如果您希望参数为字符串文字,那么您将缺少引号,并且可能还会缺少将函数本身包装起来的双引号:

onclick="updatebook(`${bm[i].id}`,`${bm[i].description}`)"

答案 1 :(得分:0)

我不知道您的数据是什么样子,所以我认为它看起来像这样:

const bm = [
    { "id": 1, "description": "this is bm1" },
    { "id": 2, "description": "this is bm2" }
];

然后,我将onclick属性值括在双引号""中,并将updatebook函数的第二个参数括在单引号中。这对我没有任何错误。

for (let i = 0; i < bm.length; i++) {
    document.getElementById('elementID').innerHTML += `<button type="button" class="btn btn-info" onclick="updatebook(${bm[i].id},'${bm[i].description}')">Update</button>`;
}