2017-11-25 30 views
-1

我曾尝试以下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,但仍然不会重定向。
+0

检查的错误/ – urfusion

+1

可能的复制你的浏览器控制台【如何重定向到另一个网页?](https://stackoverflow.com/questions/503093/how-to-redirect到另一个网页) – MaxG

回答

1

你混淆window.location.assign()方法用window.location.href属性。

window.location.href = url; // directs the page to new location 
window.location.assign(url); // navigates to a new page 

docs

  1. 而且你的逻辑有缺陷。您正在检查某个元素是否有click成员:if (action_option.elements[0].click) { ... }。这将始终返回true。所以你永远不会看到else部分执行。
  2. 不要使用多个submit按钮。让他们定期按钮。你也不需要表单,你不会提交任何内容。

做这样的事情:

function redirectToURL(url) { 
    window.location.href = url; 
} 
<button type="button" onclick="redirectToURL('URL 1')">button 1</button> 
<button type="button" onclick="redirectToURL('URL 2')">button 2</button> 
<button type="button" onclick="redirectToURL('URL 3')">button 3</button> 
+0

是的,你说得对。但是我现在试过,但它只执行'action_option.elements [0] .click',即使我点击了第二个按钮元素。你怎么看? – Reiion

+0

编辑答案.. –

+0

你是对的!谢谢你解释得很好。我正在让它比应该更难。 – Reiion

0

尝试 document.location ='URL';

1

尝试window.location.href = url;来代替。

如果你并不需要一个专门的形式,我会删除表格,做这样的事情:

<input type="submit" onclick="redirect_to_url(1)" class="btn btn-primary btn-lg btn-space col-sm-3" value="View Object"/> 
<input type="submit" onclick="redirect_to_url(2)" class="btn btn-primary btn-lg btn-space col-sm-3" value= "View Detail"/> 
<input type="submit" onclick="redirect_to_url(3)" class="btn btn-primary btn-lg btn-space col-sm-4" value="View Inventory"/> 

然后在你的Javascript:

function redirect_to_url(submitId){ 

    var url = document.URL; 
    url = url.slice(0, -1); 

    if (submitId === 1) { 
     url = url + "{% url 'objects_view' %}"; 
     alert(url); 
     window.location.href(url); 
    } 
    else if(submitId === 2) { 
     url = url + "{% url 'details_view' %}"; 
     alert(url); 
     window.location.href(url); 
    } 
    else { 
     url = url + "{% url 'inventory' %}"; 
     alert(url); 
     window.location.href(url); 
    } 
    return false; 
} 
+0

这只能删除我的控制台错误。错误地输入了这个感谢指出。但仍然不重定向 – Reiion

+0

为ya增加了一些代码! – maxpaj

+0

谢谢maxpaj! – Reiion

0

你混淆了这里。 window.location.href = url正在模拟用户点击的链接。 window.location = url正在传递用户所需的网址。 window.location.redirect(url)使用额外的布尔参数将用户重定向到所需的URL,指出用户是否应从服务器加载页面。

1

查看下面的代码,我对你的代码做了一些改变,它的工作正常。

\t function redirect_to_url(x){ 
 
    /*var action_option = document.getElementById("action_option");*/ 
 
    //alert(action_option); 
 

 
    var url = document.URL; 
 
    url = url.slice(0, -1); 
 

 

 
    if (x=="View Object") { 
 
     url = url + "{% url 'objects_view' %}"; 
 
     alert(url); 
 
\t \t //console.log("objects_view"); 
 
     window.location.href=url; 
 
    } 
 
    else if(x=="View Detail") { 
 
     url = url + "{% url 'details_view' %}"; 
 
     alert(url); 
 
\t \t //console.log("details_view"); 
 
     window.location.href=url; 
 
    } 
 
    else if(x=="View Inventory") { 
 
     url = url + "{% url 'inventory' %}"; 
 
     alert(url); 
 
\t \t //console.log("inventory"); 
 
     window.location.href=url; 
 
    } 
 
    return false; 
 
}
<form id="action_option" action=""> 
 
     <input type="button" class="btn btn-primary btn-lg btn-space col-sm-3" value="View Object" onclick="redirect_to_url(this.value)" /> 
 
     <input type="button" class="btn btn-primary btn-lg btn-space col-sm-3" value= "View Detail" onclick="redirect_to_url(this.value)"/> 
 
     <input type="button" class="btn btn-primary btn-lg btn-space col-sm-4" value="View Inventory" onclick="redirect_to_url(this.value)"/> 
 
\t </form>

相关问题