简体   繁体   English

按钮仅在单击时起作用,在输入/返回时不起作用

[英]Button only works on click, not on enter/return

I have a simple password protection set up on my website to keep out random visitors and give clients a sense of security. 我在我的网站上设置了简单的密码保护,以阻止随机访问者,并给客户一种安全感。

I understand that the password can be easily pulled from the source code, but that isn't a problem. 我知道可以很容易地从源代码中提取密码,但这不是问题。

When the password is entered, it only works if the button is clicked. 输入密码后,仅在单击按钮后才能使用。 When enter is hit (which most people default to), it just reloads the page with "?pass=password" at the end of the location. 按下enter键(大多数人默认是回车键)时,它只是在位置末尾用“?pass = password”重新加载页面。 It would be ideal if the button could be clicked and/or enter could be pressed. 如果可以单击按钮和/或可以按Enter,则将是理想的。

In head... 在头...

</script>
<!--- PASSWORD PROTECTION SCRIPT --->    
function TheLogin() {    
   var password = 'accessportfolio';    
   if (this.document.login.pass.value == password) {
      window.location.href="protected.html#portfolio";
   } else {
      location.href="incorrect.html";
   }
}    
<!--- End hiding --->
</script>

In Body... 在体内...

<form name="login" style="margin: 0px">
<INPUT TYPE="text" NAME="pass" size="17"
       onKeyDown="if(event.keyCode==13) event.keyCode=9;" 
       style="width: 250px; margin: 5px;"><br>
<input type="button" value="CLICK TO ACCESS" 
       style="width : 150px; margin: 10px"
       onClick="TheLogin(this.form)">
</form>

The button should be a submit button and you should be using onsubmit of the form and not onclick of a button. 该按钮应该是一个提交按钮,并且您应该使用表单的onsubmit而不是按钮的onclick。

 function TheLogin() { var password = 'accessportfolio'; if (this.document.login.pass.value == password) { window.location.href = "protected.html#portfolio"; } else { location.href = "incorrect.html"; } } 
 <form name="login" style="margin: 0px" onsubmit="TheLogin(); return false;"> <INPUT TYPE="text" NAME="pass" size="17" style="width: 250px; margin: 5px;"> <br> <input type="submit" value="CLICK TO ACCESS" style="width : 150px; margin: 10px"> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM