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

来源:互联网 发布:ddos攻击80端口 编辑:程序博客网 时间:2024/05/16 05:23

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

在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 keynumvar keycharvar numcheckif(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>


原创粉丝点击