Javascript 密码提交 - 表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11992332/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript Password Submit - Form
提问by MillerMedia
I am trying to set a password to a form. I basically want someone to type a password into a password field and hit submit and if the password is right, it redirects them to buybutton.php in the same window. If the password isn't right, a javascript alert pops up that says 'Password wrong'. I've tried to write out the Javascript but no action is happening when the button is clicked. What have I done wrong in my code below? Thanks for the help!
我正在尝试为表单设置密码。我基本上希望有人在密码字段中输入密码并点击提交,如果密码正确,它会将他们重定向到同一窗口中的 buybutton.php。如果密码不正确,则会弹出一个 javascript 警报,提示“密码错误”。我试图写出 Javascript 但单击按钮时没有发生任何操作。我在下面的代码中做错了什么?谢谢您的帮助!
<!DOCTYPE html>
<body width="950" height="300" style="background-image:url('giftcard_bg.jpg');">
<head>
<SCRIPT LANGUAGE="JavaScript">
function goForit() {
var passwd;
passwd = this.document.giftForm.pass.value;
if(passwd=="samsamsam"){
window.open ('buybutton.php','_self',false)
}
else{
alert('Password wrong');
}
}
</SCRIPT>
</head>
<div style="position:absolute; top:150px; left:285px; text-align:center;">
<form id="giftForm">
<input type="password" id="pass" placeholder="Enter Secret Code" style="font-size:150%;"/>
<br />
<input type="button" onClick="goForit()" value="Submit" style="font-size:150%; margin-top:15px;" />
</form>
</div>
</body>
</html>
回答by Oriol
You should use document.getElementById('pass').value
instead of this.document.giftForm.pass.value
你应该使用document.getElementById('pass').value
而不是this.document.giftForm.pass.value
function goForit() {
var passwd;
passwd = document.getElementById('pass').value;
if(passwd=="samsamsam"){
window.open ('buybutton.php','_self',false)
}
else{
alert('Password wrong');
}
}
Word of warning:
警告语:
But be aware! Everybody can see the source code and find the password!!!
但要注意!大家可以看源码找密码!!!
Instead of this, you should send the form to the server, and in the server side, check if it's correct. If it's correct, redirect the user to buybutton.php, but sending him a cookie which authorises him to enter to that page. And that page should check if he has that cookie.
取而代之的是,您应该将表单发送到服务器,并在服务器端检查它是否正确。如果正确,将用户重定向到buybutton.php,但向他发送一个cookie 以授权他进入该页面。那个页面应该检查他是否有那个 cookie。
But I'm not an expert, if you want that you should make another question asking that.
但我不是专家,如果你想要,你应该提出另一个问题。
回答by Zac
passwd = this.document.giftForm.pass.value;
causes an error try
导致错误尝试
passwd = document.getElementById('pass').value;
You were missing a ;
too after window.open ('buybutton.php','_self',false)
;
在 window.open 之后你也错过了('buybutton.php','_self',false)
回答by Rajeev Sharma
this should work
这应该有效
function goForit() {
var passwd = document.getElementById("pass").value;
if(passwd=="samsamsam"){
window.open ('buybutton.php','_self',false)
}
else{
alert('Password wrong');
}
}
回答by su-
Oriol and Rajeev's answer are correct.
Oriol 和 Rajeev 的回答是正确的。
Alternatively, you can do this:
或者,您可以这样做:
change
改变
passwd = this.document.giftForm.pass.value;
to
到
passwd = document.forms['giftForm'].pass.value;