This article describes the method of JS pressing Enter to achieve login. This function has a wide range of practical value. Share it for your reference. The specific methods are as follows:
Method 1:
<html xmlns="http://www.w3.org/1999/xhtml" ><head><title>Check Score</title><script language="JavaScript">function keyLogin(){ if (event.keyCode==13) //The key value of the Enter key is 13 document.getElementByIdx_x("input1").click(); //Login event calling the login button}</script></head><body onkeydown="keyLogin();"><input id="input1" value="Login" type="button" onClick="alert('Call successful!')"></body></html>Method 2:
<script>function KeyDown(){ if (event.keyCode == 13) { event.returnValue=false; event.cancel = true; Form1.btnsubmit.click(); }}</script>How to use:
<form name="Form1" method="">Username:<INPUT TYPE=text SIZE=20 maxlength = 8 onkeydown=KeyDown()>Password:<INPUT TYPE=password SIZE=20 maxlength = 8 onkeydown=KeyDown()><input type="submit" name="btnsubmit" value="Submit" /></form>
Method 3:
Any website page has a login interface. Many times, after entering the username and password, you have to click a button or link similar to login with the mouse. In this way, you can enter the website to do what you like to do.
Sometimes I wonder if I can just type what I should enter and then just type in the carriage and execute the login function? The solution is as follows:
ss.html page code:
<html><head><title></title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><link rel="stylesheet" href="css/text.css" type="text/css"></head><body bgcolor="#FFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" onkeydown="on_return();"> <form name ="loginForm" method="post" action="fuck.html"> <table cellpacing="0" cellpadding="0"> <tr> <td><span>Account name</span> <input type="text" name="userName" size="18.5"> </td> </tr> <tr> <td><span>Password</span> <input type="password" name="pwd" > </td> </tr> <tr> <td> <a id="sub" onClick='check()' > Log in</a></td> </tr> </table> </form></body></html><script language="javascript">function check() { var formname=document.loginForm; if (formname.userName.value == "") { alert("Please enter the user name!"); formname.userName.focus(); return false; } if (formname.pwd.value == "") { alert("Please enter the password!"); formname.pwd.focus(); return false; } formname.submit();} //When entering, the default is to log in function on_return(){ if(window.event.keyCode == 13){ if (document.all('sub')!=null){ document.all('sub').click(); } } } } }</script>Note here: In <body> we add the onkeydown attribute, so that after we enter the content, we can directly execute the JS method on_return(). Because if window.event.keyCode is 13, it means the Enter key, so we determine whether the key we press is the Enter key. If it is, we will look for the 'sub' attribute, and if it is found, it will be.
I hope that the methods described in this article will be helpful to everyone's web programming.