javascript 按钮点击到带有 location.href 的网络 uri
javascript button onclick to a web uri with location.href
我正在尝试向 Javascript 中 <button>
标记外部的按钮添加 onclick 函数。下面是我目前所拥有的,但单击时按钮不会链接到该 uri。我应该怎么做?非常感谢您的提前帮助!我也在代码中评论了。
对于我有的按钮:
"<td><button"+" id="+foodList[i].Id +" onClick="+"\"doSomething(this.id)\""+" >"+"Get"+"</button></td></tr>"
所以基本上我在 for 循环中为 "Get" 按钮分配了一个 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 + "';"
}
改变这个:
document.getElementById(id).onclick="location.href='"+uri+"';"
像这样:
document.getElementById(id).onclick= function(){
location.href = "Wanted url here"; // location.href = location.href + "/currentpath/additional/params/here"
}
这样,当您单击该按钮时,您就为其附加了一个功能,它会更改 url(重定向页面)
你必须这样写:
document.getElementById(id).onclick = function() {
location.href = uri;
}
我正在尝试向 Javascript 中 <button>
标记外部的按钮添加 onclick 函数。下面是我目前所拥有的,但单击时按钮不会链接到该 uri。我应该怎么做?非常感谢您的提前帮助!我也在代码中评论了。
对于我有的按钮:
"<td><button"+" id="+foodList[i].Id +" onClick="+"\"doSomething(this.id)\""+" >"+"Get"+"</button></td></tr>"
所以基本上我在 for 循环中为 "Get" 按钮分配了一个 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 + "';"
}
改变这个:
document.getElementById(id).onclick="location.href='"+uri+"';"
像这样:
document.getElementById(id).onclick= function(){
location.href = "Wanted url here"; // location.href = location.href + "/currentpath/additional/params/here"
}
这样,当您单击该按钮时,您就为其附加了一个功能,它会更改 url(重定向页面)
你必须这样写:
document.getElementById(id).onclick = function() {
location.href = uri;
}