JS按回车键实现登录的方法
Posted maxlg
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JS按回车键实现登录的方法相关的知识,希望对你有一定的参考价值。
按回车实现登录的方法可用js来进行实现,有以下三种方法:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">(此代码为浏览器以中文的形式呈现)
方法一:
<
html
>
<
head
>
<
title
>Check Score</
title
>
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属性,这样我们输入好内容后按键就可以直 接执行JS方法on_return()了.因为window.event.keyCode 如果是13的话,表示回车键,所以我们判断我们按的键是不是回车键,如果是我们就去找‘sub‘属性,假如找到了执行click方法就是了。
(以上三种干方法均可以实现回车登录,)
以上是关于JS按回车键实现登录的方法的主要内容,如果未能解决你的问题,请参考以下文章