[英]Javascript will not move to the next webpage after user validation in a Spring Framework
在讓用戶移動到網頁的另一部分之前,我嘗試進行簡單的驗證檢查。 不用擔心,這只是設置一個簡單的版本,我永遠不會在js中將密碼和用戶名作為變量讓所有人看到。 這只是一個起點,可以測試我的URL在我的網頁中的流向。
我的代碼有兩個問題。 因此,當我輸入正確的用戶名並傳遞時,它可以正常工作並說重定向,但它不會重定向到網頁。 它只是重新加載登錄頁面,卻無處可去。
第二個問題是,當我輸入錯誤的密碼時,嘗試計數永遠不會低於2。
感謝您對此的任何幫助。
這是我正在使用的腳本
<html>
<head>
<script>
var attempt = 3; // Variable to count number of attempts.
// Below function Executes on click of login button.
function validate(){
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
if ( username == "gus" && password == "cool"){
alert ("Login successfully");
window.location.href = "/hub/hubpage"; // Redirecting to other page.
return false;
}
else{
attempt --;// Decrementing by one.
alert("You have left "+attempt+" attempt;");
// Disabling fields after 3 attempts.
if( attempt == 0){
document.getElementById("username").disabled = true;
document.getElementById("password").disabled = true;
document.getElementById("submit").disabled = true;
return false;
}
}
}
</script>
</head>
<body>
<h2>Login Page </h2>
<a href="/hub/hubpage">Hub</a>
<br/>
<br/>
<form action = "">
<fieldset>
<legend>Login Infomration</legend>
<label>User Name :</label><br/>
<input type="text" name="username" id="username"/><br/>
<label>Password :</label><br/>
<input type="password" name="password" id="password"/><br/>
<br/>
<input type="submit" value ="Login" onclick ="validate()">
</fieldset>
</form>
</body>
</html>
href中心頁可以很好地用作鏈接,所以我知道它可以到達那里
試試下面的代碼:
<script>
var attempt = 3; // Variable to count number of attempts.
// Below function Executes on click of login button.
function validate(){
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
if ( username == "gus" && password == "cool"){
alert ("Login successfully");
window.location.href = "/hub/hubpage"; // Redirecting to other page.
return false;
}
else{
attempt --;// Decrementing by one.
alert("You have left "+attempt+" attempt;");
// Disabling fields after 3 attempts.
if( attempt == 0){
document.getElementById("username").disabled = true;
document.getElementById("password").disabled = true;
document.getElementById("submit").disabled = true;
}
return false;
}
}
</script>
代替window.location
使用window.location.href
<script>
var attempt = 3; // Variable to count number of attempts.
// Below function Executes on click of login button.
function validate(){
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
if ( username == "gus" && password == "cool"){
alert ("Login successfully");
window.location.href = "/hub/hubpage"; // Redirecting to other page.
return false;
}
else{
attempt --;// Decrementing by one.
alert("You have left "+attempt+" attempt;");
// Disabling fields after 3 attempts.
if( attempt == 0){
document.getElementById("username").disabled = true;
document.getElementById("password").disabled = true;
document.getElementById("submit").disabled = true;
return false;
}
}
}
</script>
成功驗證后,刪除return false; 聲明,因為這阻止了表單的提交。
function validate(){
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
if ( username == "gus" && password == "cool"){
alert ("Login successfully");
window.location.href = "/hub/hubpage"; // Redirecting to other page.
//return false;
}
else{
attempt --;// Decrementing by one.
alert("You have left "+attempt+" attempt;");
// Disabling fields after 3 attempts.
if( attempt == 0){
document.getElementById("username").disabled = true;
document.getElementById("password").disabled = true;
document.getElementById("submit").disabled = true;
return false;
}
}
}
對於第二個問題,我不確定哪里出了問題,因為這對我來說很好。
使用location.assign(“ http://www.w3schools.com ”); 用於從登錄頁面重定向
對於第一個問題,將輸入類型更改為按鈕。 <input type="button" value ="Login" onclick ="validate();">
對於第二個問題,請嘗試try attempt = attempt - 1;
try attempt = attempt - 1;
該代碼正在工作...
<html>
<head>
<script>
var attempt = 3;
function validate(){
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
if ( username == "gus" && password == "cool"){
alert ("Login successfully");
window.location = "/hub/hubpage";
}
else{
attempt--;
alert("You have left " + attempt + " attempt;");
if( attempt == 0){
document.getElementById("username").disabled = true;
document.getElementById("password").disabled = true;
document.getElementById("submit").disabled = true;
}
}
return false;
}
</script>
</head>
<body>
<h2>Login Page </h2>
<a href="/hub/hubpage">Hub</a>
<br/>
<br/>
<form action = "" onsubmit="return validate()">
<fieldset>
<legend>Login Infomration</legend>
<label>User Name :</label><br/>
<input type="text" name="username" id="username"/><br/>
<label>Password :</label><br/>
<input type="password" name="password" id="password"/><br/>
<br/>
<input type="submit" value ="Login">
</fieldset>
</form>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.