简体   繁体   English

使用回车键和“提交”按钮输入密码(HTML / Javascript)

[英]Enter a password using BOTH enter key and “Submit” button (HTML/Javascript)

I'm relatively new to making websites, however, I am trying to use HTML/Javascript so the user can enter a password using either the ENTER key or the "Submit" button, whichever the user chooses. 我是制作网站的新手,但是,我尝试使用HTML / Javascript,以便用户可以使用ENTER键或“ Submit”(输入)按钮输入密码,无论用户选择哪个。 My code works when I simply print a message like "Welcome!" 当我仅打印诸如“ Welcome!”之类的消息时,我的代码即可工作。 but when I want to redirect them to another page like "intro.html," thats when it stops working. 但是当我要将它们重定向到另一个页面(如“ intro.html”)时,多数民众赞成在它停止工作时。 I have attached my code. 我已附上我的代码。 I understand event.keyCode may not be javascript and may be jquery? 我了解event.keyCode可能不是javascript,可能是jquery? If so, please let me know how to implement jquery into html or any other ideas you all may have :) Thank you so much! 如果是这样,请让我知道如何将jquery实现为html或大家可能有的任何其他想法:)非常感谢!

Log In 登录

<script language = "JavaScript">
    function showPass(form){

        var pass = form.pwd.value;
        var pass1 = "tanwebdesign"
        var pass2 = event.keyCode || event.which;

        if(pass == pass1 || ((pass == pass1) && pass2 == 13))
        {
            window.location.replace("intro.html");
        }
        else{
            window.open = "LogIn.html"
        }
    }
</script>

<form>

    Password: <input type = "password" name = "pwd" /> 

        <input type = "submit" style = "display: none"  onclick = "showPass(form)" />

        <input type = "button" value = "Log In" onclick = "showPass(form)" />
</form>

This how it can be done 这是怎么做的

  • No need to add two input s makes the same action 无需添加两个input s即可执行相同的操作
  • Use e.preventDefault() to prevent form's default redirect .. see function showPass(form , e) and in html onSubmit = "showPass(this , event)" you can use onSubmit for a form instead of onClick for a submit input 使用e.preventDefault()防止表单的默认重定向。请参见function showPass(form , e)并在html onSubmit = "showPass(this , event)" ,可以将onSubmit用于表单,而不是onClick来提交输入
  • No need to catch the ENTER keypress it will work automatically with a form 无需按ENTER键,它将自动与表格一起使用
  • To redirect the page you can use window.location.href = 'https://www.google.com' 要重定向页面,您可以使用window.location.href = 'https://www.google.com'

 function showPass(form , e){ e.preventDefault(); var pass = form.pwd.value; var pass1 = "tanwebdesign"; alert(pass); if(pass == pass1 || pass == pass1) { //window.location.replace("intro.html"); } else{ //window.open = "LogIn.html" } } 
 <form onSubmit = "showPass(this , event)"> Password: <input type = "password" name = "pwd" /> <input type = "submit" value = "Log In" /> </form> 

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

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