2017-04-16 90 views
-1

我正在一個簡單的登錄頁面。當用戶輸入正確的用戶名和密碼時,會將其重定向到另一頁。到目前爲止,我曾嘗試:Javascript的window.open工作,但window.location不

window.location的window.location.replacewindow.hrefwindow.open是唯一可行的是window.open,但是我試圖使它不會打開一個新的選項卡,它只是將它們重定向到另一個頁面。

的Html

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <link rel="stylesheet" href="master.css"> 
    <title>Day Four</title> 
</head> 
<body> 
<h1>Log In</h1> 

<form onsubmit="valid()"> 
<label for="username">Username:</label> 
<input id = 'user-name' type="text" name="" value="" required> 

<label for="password">Password:</label> 
<input id = 'pass-word' type="password" name="" value="" required> 

<input id = 'log-in'type="submit" name="" value="Log In"> 
</form> 

</div> 

</body> 
<script src="mainJs.js" charset="utf-8"></script> 
</html> 

JS

function valid(){ 
var username = document.querySelector('#user-name').value; 
var password = document.querySelector('#pass-word').value; 

if(username === 'test' && password === '1'){ 
    window.open("gradebook.html") 

}else{ 
    alert("Wrong username or password") 
} 

} 

編輯:我知道這並不安全。我把運行window.open(「gradebook.html」)的代碼看成其他人不做任何事情。 當使用其他控制檯時,我也不會在控制檯中發現任何錯誤。

+0

當您列出三件不起作用的東西時,我們無法告訴您您做錯了什麼,但只會告訴我們您如何使用適用的東西。 – Quentin

+0

'for =「username」'和'id ='user-name''不匹配。使用[驗證器](https://validator.w3.org/nu/)。 – Quentin

+0

http://stackoverflow.com/a/506004/7733724 – Sam

回答

-1

您只需要將window.location設置爲新的URL即可。

document.querySelector("button").addEventListener("click", function(){ 
 
    window.location = "http://cnn.com"; 
 
});
<button>Click Me</button>

+2

沒有理由拒絕投票。答案是正確的。 –

+1

不是downvoter,但OP說他們嘗試過。我知道...... OP沒有顯示他們試過的東西 –

+0

@JuanMendes OP沒有正確地嘗試。這是正確的答案。 –

0

它看起來像問題是與窗體標籤。如果我刪除表單標籤,所有東西都應該開始工作。