2013-09-24 81 views
0

因此,我有一個「創建帳戶」窗體,其中包含年齡輸入。我的年齡要求設置爲16.我想要做的是,如果該人年齡在16歲以下,它會發出警告消息,然後當用戶單擊警報窗口上的確定按鈕時,它會將它們發送回索引頁。 以下是我對JS代碼:如何使用javascript將用戶發送到其他頁面

if (age == null || age == "") 
{ 
alert("Please enter your age to continue."); 
return false; 
} 
else if (age == isNaN(age)) 
{ 
alert("Age input was not a number!\nPlease enter your age to continue."); 
return false; 
} 
else if (age < 16) 
{ 
alert("You are not old enough to have an account.\n If you have concerns or questions about our age policy please send them to [email protected]"); 
return false; 
} 

基本上,當它運行的是最後的「否則,如果」語句,發現輸入小於16,我希望它的用戶重定向到的index.html(顯示警告消息之後的頁面。

我已經嘗試使用:

else if (age < 16) 
{ 
alert("You are not old enough to have an account.\n If you have concerns or questions about our age policy please send them to [email protected]"); 
location.assign("index.html"); 
return false; 
} 

我也曾嘗試location.href但既不工作。任何幫助,將不勝感激!

+0

以防萬一任何人想知道這個「創建帳戶」是在它自己的html頁面上,名爲create.html – Thone2

回答

1

只需爲location屬性指定一個新值即可。 location = "index.html"

0

使用此:

window.location.href = "your location"; 
-1

最簡單的可能爲:

window.location = "http://www.google.com/" 
0

你應該發佈你的html內容,因爲你可能有問題。前一段時間,我遇到了這個問題,當年齡低於規定的年齡限制時,我被引導到一個我從未用window.location.href指定的網站,即使它被分配到不同的網站,問題可能會發生因爲你已經在它周圍包裹了一個錨標籤,所以它會引導你到你在html頁面上指定的站點,我建議你檢查一下你沒有一個錨點標籤指引你在你的html頁面的其他地方!

相關問題