登录不起作用
Logging in is not working?
尊敬的Stackoverpowers,
对于学校,我正在制作一个简单的社交媒体网站,我需要证明你可以登录。我对Javascript没有那么多经验,所以我试图将我在youtube和w3school上找到的知识和信息结合起来,并带来了这个。只有当我按下登录按钮时,它才不会做任何事情(据我所见)。
附言:我知道这个网站真的很难看,但它的一些特定功能正在发挥作用:3
var count= 2;
function validate(){
var username = document.login.username.value;
var password = document.login.password.value;
var valid= false;
var usernameArray = ["Gdp", "Hekman", "Koning", "ErikHekman", "ThijsWaardenburg", "Ronald", "RonaldVanEssen" ];
var passwordArray = ["Gdp123", "Hekman123", "Koning123", "ErikHekman123", "ThijsWaardenburg123", "Ronald123", "RonaldVanEssen123"];
for (var i = 0; i < usernameArray.length; i++)
{
if ((username == usernameArray[i]) && (password == passwordArray[i]))
{
valid = true;
break;
}
}
if (valid)
{
alert("Log in was succesfull!")
window.location = "timeline";
return false;
}
var again = "tries";
if (count ==1)
{
again = "try"
}
if (count >= 1)
{
alert("Wrong password or username")
count--;
}
else
{
alert("Too many attempts");
document.login.username.value = "You are now blocked";
document.login.pass.value = "You are now blocked";
document.login.username.disabled = true;
document.login.password.disabled = true;
return false;
}
}
html {font-family:Arial, Helvetica, sans-serif; color:#333;}
body { background:#CCC; margin:0; }
#container { width:900px; margin:0 auto; background:#FFF; }
#header { width:100%; height:60px; border-bottom:1px solid #c7c7c7; background:#3f0b04; text-align:center; }
#header h1 {color:#e7c321; }
#logo { float:left; width:40px; height:40px; margin:10px; background:#CCC;}
#navbar { height:36px; clear:both; background-color:#e7c321; }
#navbar ul { margin:0; padding:0; list-style-type:none; }
#navbar ul li { padding:10px; float:left; }
#navbar ul li a { font size:12px; float:left; padding:0 0 0 20px; display:block; }
#banner { background:#3f0b04; heigth:120px; clear:both; padding:20px; text-align:center; color:#FFF; margin:10px 0 2px 0;}
#middle-column { float:left; width:858px; height:500px; border:1px solid #333; color:#000; background:#bcbab4; padding:20px; text-align:center; }
#login-logo {width:100%; ; height:200px; float:center;}
#login-form {float:center; width:175px; }
#submit {width:170px; float:center;}
#register {width:170px; float:center;}
#footer { padding:20px; clear:both; color:#FFF; background-color:#3f0b04; text-align:center; }
<div id="container">
<!-- HEADER -->
<div id="header">
<div id="logo"><img src="http://imgur.com/578H3e8.png"></img></div>
<h1>BroCialize</h1>
<div id="navbar">
<ul>
<li><a href="url"></a></li>
<li><a href="url"></a></li>
<li><a href="url"></a></li>
</ul>
</div>
</div>
<!-- CONTENT AREA -->
<div id="content-area">
<div id="banner">This site is Legen...wait for it...Dary</div>
<div id="middle-column">
<div id="login-logo"><img src="http://imgur.com/uelX4ie.png></img></div>
<div id="login-form">
<form method="POST">
<input type="text" name="username" placeholder="username" />
<br>
<input type="password" name="password" placeholder="password" />
<br>
</form>
<button id="submit">Log in</button>
<br>
<button id="register">Register</button>
</div>
</div>
</div>
<!--FOOTER -->
<div id="footer">Copyright by BroCialize - 2016</div>
</div> <!-- END CONTAINER -->
很抱歉,如果我问新手问题,我只是在寻求一些帮助。提前感谢
-iWnB
正如我所看到的,找不到"document.login.username",因为网站上没有"login"命名表单。因此,函数找不到在哪里检查用户名的值。
至少目前您的代码中有两个主要问题。
首先,在JS部分中,您使用的是document.login...
,而没有具有id="login"
的HTML元素(可能是您的<form>
)
因此,当前您尝试处理不存在的元素。
另一方面,直接关系到你的问题
当我按下登录按钮时,它没有做任何事情
- 在任何情况下,当您按下按钮时,它所指向的表单都会被提交。这意味着调用了表单的
action
属性中声明的url - 但是,由于您没有指定任何
action
属性,因此默认情况是再次调用当前页面
所以是的,你什么都没看到。。。但如果你仔细观察它,你会注意到浏览器的选项卡很快显示了它的等待轮,因此证明它调用了一个页面
在#1之后,您可能需要编写一个专用的PHP脚本(或将其包含在当前页面中)来完成一些响应表单提交的工作
但在当前的情况下,您想要的只是对按钮按下做出反应,以便执行validate()
功能
要实现这一目标,主要有两种方法:
-
旧的方式是:向
<button>
元素添加一个onclick
属性。现在不建议这样做,所以我甚至不告诉你如何做。 -
现代时尚,就是在你的按钮上附加一个事件监听器,就像这样:
document.onload = function() { document.getElementById('submit').addEventListener('click', validate); }
这段代码必须在validate()
函数之前或之后添加到JS部分。
- 我的点击登录在网页上不起作用
- 意思是.js - 登录后重定向用户 - 在本地工作,但在部署时不起作用
- 科尔多瓦脸书登录FB.api调用不起作用
- PHP登录不起作用
- 登录后PHP登录脚本重定向不起作用
- 如果用户登录不起作用,则运行脚本
- 会话在登录页不起作用
- JSP-Javascript-JSP 登录流程不起作用
- $.mobile.changePage() 登录后不起作用
- Facebook 登录在 CN1 JavaScript 端口中不起作用
- Phonegap/Cordova facebook 登录在已经连接时不起作用
- Facebook 登录重定向 PHP/Javascript 不起作用
- HTML 登录不起作用
- $location.path() 在 facebook 登录后不起作用
- 谷歌加登录按钮已呈现,但不起作用
- 使用 JavaScript 启动 Google+ 登录流程不起作用
- 使用 Angular JS 登录,$http请求不起作用
- Facebook自动登录JS在控制台中工作,但在GM脚本中不起作用
- FB.登录响应会话不起作用
- 导航栏折叠在登录模板上不起作用