我曾尝试以下JavaScript页面装载机的努力有什么不工作的缘故:JavaScript不重定向到URL在if-else的情况下
window.location.href = url
window.location = url
window.location.assign(url)
window.location.redirect(url)
他们没有一个人会向正确的页面。每当我点击按钮时,即使点击第二个按钮,它仍然会捕获第一个if
案例。我是否在我的if-else语句或重定向它的方式上做错了什么?
我也试过在做onclick=
,配对switch case
,window.location.href
还是没有通过。
我的HTML:
<form id="action_option" onsubmit="redirect_to_url()" action="">
<input type="submit" class="btn btn-primary btn-lg btn-space col-sm-3" value="View Object"/>
<input type="submit" class="btn btn-primary btn-lg btn-space col-sm-3" value= "View Detail"/>
<input type="submit" class="btn btn-primary btn-lg btn-space col-sm-4" value="View Inventory"/>
</form>
我的javascript:
function redirect_to_url(){
var action_option = document.getElementById("action_option");
alert(action_option);
var url = document.URL;
url = url.slice(0, -1);
if (action_option.elements[0].click) {
url = url + "{% url 'objects_view' %}";
alert(url);
window.location.href(url);
}
else if(action_option.elements[1].click) {
url = url + "{% url 'details_view' %}";
alert(url);
window.location.href(url);
}
else {
url = url + "{% url 'inventory' %}";
alert(url);
window.location.href(url);
}
return false;
}
编辑:
控制台错误 -
TypeError: window.location.href is not a function
- 删除错误改为
window.location.href = url
,但仍然不会重定向。
检查的错误/ – urfusion
可能的复制你的浏览器控制台【如何重定向到另一个网页?](https://stackoverflow.com/questions/503093/how-to-redirect到另一个网页) – MaxG