2017-08-12 42 views
-1

我只需要我的表單就可以在使用javascript驗證後將用戶重定向到另一個頁面。Javascript:我在哪裏添加重定向?

這裏是我的代碼:

function checkEnq() { 
    var x = document.forms["enq"]["email"].value; 
    var y = document.forms["enq"]["dB"].value; 
    var z = document.forms["enq"]["textF"].value; 
    if (x == null || x == "" || y == null || z == null || z == "") { 
     alert("Please make sure all fields are entered."); 
    } 
} 

如何添加的location.hrefwindow.open一旦JS已經檢查了所有的字段(電子郵件,分貝,& textF)填充?

我可以將target="_blank" action="./result.html"添加到<form>字段,即使表格未填滿,它仍會重定向到結果頁面。

+1

你不能在別的塊添加重定向? – Winnie

回答

0

添加一個「其他」塊

function checkEnq() { 
    var x = null; 
    var y = null; 
    var z = null; 
    try { 
      x = document.forms["enq"]["email"].value; 
      y = document.forms["enq"]["dB"].value; 
      z = document.forms["enq"]["textF"].value; 
    } catch(e) { 
     alert('code error'); 
    }; 

    if (x == null || x == "" || y == null || z == null || z == "") { 
     alert("Please make sure all fields are entered."); 
    } else { 
     document.location.href="/redirect.page" 
    } 
} 
+0

我做到了,它不會將我重定向到我的頁面:(@pagetronic – Bryan

+0

驗證電子郵件,dB和textF是否以屬性名稱作爲標識存在 – pagetronic

0

我解決它通過使用這個window.open("./page.html")代替document.location.href