当前位置: 首页 > 图文教程 > 网络编程 > Javascript > javascript 按回车键相应按钮提交事件

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 javascript 按回车键相应按钮提交事件


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 244 ::
收藏到网摘: n/a

在表单提交前经常遇到表单验证问题而常用的方法有以下两种. 1、使用提交按钮提交,在form表单中的onsubmit事件进行表单验证:
复制代码 代码如下:

<script type="text/javascript">
function onSub(){
//表单验证代码
}
</script>
<form action="" method="" onsubmit="javascript:onSub();">

2、使用button或图片的onclick事件调用表单验证代码:
复制代码 代码如下:

<input type="button" name="button" id="button" onclick="javascript:onSub();" />
<img src="图片路径" onclick="javascript:onSub();" />

但是,使用第二种方式进行提交表单时,不能在填写完表单时,按回车不能提交表单。这给客户的
感受是和直接使用提交按钮提交表单是不同的;为了实现此项功能只需在你的页面加上如下javascript代码即可
复制代码 代码如下:

function butOnClick() {
if (event.keyCode == 13) {
var button = document.getElementById("bsubmit"); //bsubmit 为botton按钮的id
button.click();
return false;
}
}

在你表单最有一个输入项中出发onkeydown事件调用butOnClick()函数即可;例如:登陆程序的最后一项是密码,则
<input type="password" name="userPwd" onkeydown="javascript:butOnClick();" />
这样当你输入完密码后,按下回车键即可实现表单验证和登录操作(如果用户名和密码正确)。这是个人的一点总结,共享出来
大家齐分享!