在Spring框架中进行用户验证后,Javascript不会移动到下一个网页
在我允许用户移动到我网页的另一部分之前,我试图进行一次简单的验证检查。别担心,这只是为了设置一个简单的版本,我永远不会把密码和用户名作为js中的变量让所有人看到。这只是一个起点,用来测试我在网页中的url流 我的代码有两个问题。因此,当我输入正确的用户名并传入时,它会工作并说重定向,但它不会重定向到网页。它只需重新加载登录页面,就可以不去任何地方 第二个问题是,当我输入错误的密码时,尝试次数永远不会低于2 谢谢你在这方面的帮助 这是我正在使用的脚本在Spring框架中进行用户验证后,Javascript不会移动到下一个网页,javascript,html,spring,spring-mvc,Javascript,Html,Spring,Spring Mvc,在我允许用户移动到我网页的另一部分之前,我试图进行一次简单的验证检查。别担心,这只是为了设置一个简单的版本,我永远不会把密码和用户名作为js中的变量让所有人看到。这只是一个起点,用来测试我在网页中的url流 我的代码有两个问题。因此,当我输入正确的用户名并传入时,它会工作并说重定向,但它不会重定向到网页。它只需重新加载登录页面,就可以不去任何地方 第二个问题是,当我输入错误的密码时,尝试次数永远不会低于2 谢谢你在这方面的帮助 这是我正在使用的脚本 <html> <head&g
<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>
变量尝试=3;//用于计算尝试次数的变量。
//单击“登录”按钮即可执行下面的函数。
函数验证(){
var username=document.getElementById(“用户名”).value;
var password=document.getElementById(“密码”).value;
如果(用户名==“gus”&&password==“cool”){
警报(“登录成功”);
window.location.href=“/hub/hubpage”;//重定向到其他页面。
返回false;
}
否则{
尝试--;//递减一。
警报(“您已离开“+尝试+”尝试;”;
//尝试3次后禁用字段。
如果(尝试==0){
document.getElementById(“用户名”).disabled=true;
document.getElementById(“密码”).disabled=true;
document.getElementById(“提交”).disabled=true;
返回false;
}
}
}
登录页面
登录信息
用户名:
密码:
href The hubbage作为链接运行良好,因此我知道它可以到达那里请尝试以下代码:
<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>
变量尝试=3;//用于计算尝试次数的变量。
//单击“登录”按钮即可执行下面的函数。
函数验证(){
var username=document.getElementById(“用户名”).value;
var password=document.getElementById(“密码”).value;
如果(用户名==“gus”&&password==“cool”){
警报(“登录成功”);
window.location.href=“/hub/hubpage”;//重定向到其他页面。
返回false;
}
否则{
尝试--;//递减一。
警报(“您已离开“+尝试+”尝试;”;
//尝试3次后禁用字段。
如果(尝试==0){
document.getElementById(“用户名”).disabled=true;
document.getElementById(“密码”).disabled=true;
document.getElementById(“提交”).disabled=true;
}
返回false;
}
}
请尝试以下代码:
<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>
变量尝试=3;//用于计算尝试次数的变量。
//单击“登录”按钮即可执行下面的函数。
函数验证(){
var username=document.getElementById(“用户名”).value;
var password=document.getElementById(“密码”).value;
如果(用户名==“gus”&&password==“cool”){
警报(“登录成功”);
window.location.href=“/hub/hubpage”;//重定向到其他页面。
返回false;
}
否则{
尝试--;//递减一。
警报(“您已离开“+尝试+”尝试;”;
//尝试3次后禁用字段。
如果(尝试==0){
document.getElementById(“用户名”).disabled=true;
document.getElementById(“密码”).disabled=true;
document.getElementById(“提交”).disabled=true;
}
返回false;
}
}
而不是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>
变量尝试=3;//用于计算尝试次数的变量。
//单击“登录”按钮即可执行下面的函数。
函数验证(){
var username=document.getElementById(“用户名”).value;
var password=document.getElementById(“密码”).value;
如果(用户名==“gus”&&password==“cool”){
警报(“登录成功”);
window.location.href=“/hub/hubpage”;//重定向到其他页面。
返回false;
}
否则{
尝试--;//递减一。
警报(“您已离开“+尝试+”尝试;”;
//尝试3次后禁用字段。
如果(尝试==0){
document.getElementById(“用户名”).disabled=true;
document.getElementById(“密码”).disabled=true;
document.getElementById(“提交”).disabled=true;
返回false;
}
}
}
而不是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>
变量尝试=3;//用于计算尝试次数的变量。
//单击“登录”按钮即可执行下面的函数。
函数验证(){
var username=document.getElementById(“用户名”).value;
var password=document.getElementById(“密码”).value;
如果(用户名==“gus”&&password==“cool”){
警报(“登录成功”);
window.location.href=“/hub/hubpage”;//重定向到其他页面。
返回false;
}
否则{
尝试--;//递减一。
警报(“您已离开“+尝试+”尝试;”;
//尝试3次后禁用字段。
如果(尝试==0){
document.getElementById(“用户名”).disabled=true;
document.getElementById(“密码”).disabled=true;
document.getElementById(“提交”).disabled=true;
返回false;
}
}
}
验证成功后,删除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;
}
}
}
对于第二个问题,我不确定哪里出了问题,因为我觉得它很好。验证成功后,删除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(“”);
要从登录页面重定向,请使用location.assign(“”);
对于从登录页面重定向,此代码正在运行