本文分享按回車鍵實現登錄的幾種方法,這一功能有著廣泛的實用價值。
<html xmlns="https://www.w3.org/1999/xhtml" >
<head>
<title>Check Score</title>
<script language="JavaScript">
function keyLogin(){
if (event.keyCode==13) //回車鍵的鍵值為13
document.getElementByIdx_x("input1").click(); //調用登錄按鈕的登錄事件
}
</script>
</head>
<body onkeydown="keyLogin();">
<input id="input1" value="登錄" type="button" onClick="alert('調用成功!')">
</body>
</html> <script>
function KeyDown() {
if (event.keyCode == 13) {
event.returnValue=false;
event.cancel = true;
Form1.btnsubmit.click();
}
}
</script>使用方法:
<form name="Form1" method=""> 用戶名:<INPUT TYPE=text SIZE=20 maxlength = 8 onkeydown=KeyDown()> 密碼:<INPUT TYPE=password SIZE=20 maxlength = 8 onkeydown=KeyDown()> <input type="submit" name="btnsubmit" value="提交" /> </form>
幾乎每個網站都有登錄界面,很多時候輸入好用戶名和密碼後,還得用鼠標點一下登錄按鈕。其實我們完全可以做到輸入完畢後直接敲回車執行登錄。
<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="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" onkeydown="on_return();">
<form name ="loginForm" method="post" action="fuck.html">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="69%" height="30"><span class="font_04">帳戶名</span>
<input type="text" name="userName" size="18.5">
</td>
</tr>
<tr>
<td width="69%" height="30"><span class="font_04">密 碼</span>
<input type="password" name="pwd" >
</td>
</tr>
<tr>
<td width="31%" height="30">
<a id="sub" onClick='check()' > 登陸</a></td>
</tr>
</table>
</form>
</body>
</html>
<script language="javascript">
function check() {
var formname=document.loginForm;
if (formname.userName.value == "") {
alert("請輸入用戶名!");
formname.userName.focus();
return false;
}
if (formname.pwd.value == "") {
alert("請輸入密碼!");
formname.pwd.focus();
return false;
}
formname.submit();
}
//回車時,默認是登陸
function on_return(){
if(window.event.keyCode == 13){
if (document.all('sub')!=null){
document.all('sub').click();
}
}
}
</script>此處注意:在 <body> 中加了 onkeydown 屬性,這樣輸入完內容後按鍵就能直接執行 on_return()。因為 window.event.keyCode 為 13 時表示回車鍵,我們判斷是否為回車鍵,若是則找到 id 為 sub 的元素並執行其 click 方法即可。