本文实例讲述了JS按回车键实现登录的方法,该功能有着非常广泛的实用价值。分享给大家供大家参考之用。具体方法如下:
方法一:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
< html xmlns = "http://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 > |
方法二:
1
2
3
4
5
6
7
8
9
10
11
|
<script> function KeyDown() { if (event.keyCode == 13) { event.returnValue= false ; event.cancel = true ; Form1.btnsubmit.click(); } } </script> |
使用方法:
1
2
3
4
5
|
< 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 > |
方法三:
任何一个网站页面都有登陆界面,很多时候在输入好用户名和密码后,还要用鼠标去点一个类似于登陆什么的按钮或者链接.这样你才能进网站做你喜欢做的事情.
有时候我就在想是不是能在输入好我该输入的东西后,直接敲回车就执行登陆功能呢?解决方法如下:
ss.html页面代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
< 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属性,这样我们输入好内容后按键就可以直接执行JS方法on_return()了.因为window.event.keyCode 如果是13的话,表示回车键,所以我们判断我们按的键是不是回车键,如果是我们就去找'sub'属性,假如找到了执行click方法就是了。
希望本文所述方法对大家的web程序设计有所帮助。