【发布时间】:2015-10-12 08:31:49
【问题描述】:
我正在尝试在 Javascript 中将 onclick 函数添加到 <button> 标记之外的按钮。以下是我目前拥有的内容,但单击该按钮时不会链接到该 uri。我该怎么做?非常感谢您提前提供的帮助!我也在代码中注释了。
对于我拥有的按钮:
"<td><button"+" id="+foodList[i].Id +" onClick="+"\"doSomething(this.id)\""+" >"+"Get"+"</button></td></tr>"
所以基本上我在 for 循环中为“获取”按钮分配了一个 id。
function doSomething(id) { //the button's id
var item = document.getElementById("type").value; //get some value from elsewhere in the page
if (item == "food") {
var uri = "baseuri" + id;
document.getElementById(id).onclick = "location.href='" + uri + "';"//add an onclick to the button so that it will takes the user to that uri
} else {
var uri = "another baseuri" + id;
document.getElementById(id).onclick = "location.href='" + uri + "';"
}
【问题讨论】:
标签: javascript html button onclick uri