现在的位置: 首页 > 综合 > 正文

onkeydown 解决按回车键直接提交方案

2013年01月15日 ⁄ 综合 ⁄ 共 1001字 ⁄ 字号 评论关闭

登陆页面需要扑捉用户按下回车自动提交的需求:

在body里添加onkeydown事件 跳javascript  在提交表单。

查找文档如下

onkeydown 事件会在用户按下一个键盘按键时发生。

语法:onkeydown="SomeJavaScriptCode"

支持该事件的html标签;

<a>, <acronym>, <address>, <area>, <b>, <bdo>, <big>, <blockquote>, <body>, <button>, <caption>, <cite>, <code>, <dd>, <del>, <dfn>, <div>, <dt>, <em>, <fieldset>, <form>, <h1> to <h6>, <hr>, <i>, <input>, <kbd>, <label>, <legend>, <li>, <map>, <object>,
<ol>, <p>, <pre>, <q>, <samp>, <select>, <small>, <span>, <strong>, <sub>, <sup>, <table>, <tbody>, <td>, <textarea>, <tfoot>, <th>, <thead>, <tr>, <tt>, <ul>, <var>

支持该事件的javascript对象:

document, image, link, textarea

浏览器差异:

Internet Explorer 使用 event.keyCode 取回被按下的字符,而 Netscape/Firefox/Opera 使用 event.which。

实例:在本例中,用户无法在输入框中键入数字

<html>
<body>
<script type="text/javascript">
function noNumbers(e)
{
var keynum
var keychar
var numcheck

if(window.event) // IE
  {
  keynum = e.keyCode
  }
else if(e.which) // Netscape/Firefox/Opera
  {
  keynum = e.which
  }

keychar = String.fromCharCode(keynum)
numcheck = /\d/
return !numcheck.test(keychar)
}
</script>

<form>
<input type="text" onkeydown="return noNumbers(event)" />
</form>

</html>

抱歉!评论已关闭.