jsp中如何实现按下回车键自动提交表单

所属分类: 网络编程 / JSP编程 阅读数: 969
收藏 0 赞 0 分享

为了省事很多时候希望可以按回车键来提交表单,要控制这些行为,可以借助JS来达到要求。

代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
 pageEncoding="UTF-8"%>
<%@ include file="../../common/include_tag.jsp"%>
<%@ include file="../../common/page_var.jsp"%>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><%=pageTitle%>-用户登录</title>
<%@ include file="../../common/page_head.jsp"%>
<link rel="stylesheet" type="text/css"
 href="<s:url value='/css/common_green.css'/>" />
<script type="text/javascript" src="<s:url value='/js/jquery.min.js'/>"></script>
<script type="text/javascript" src="<s:url value='/js/common.js'/>"></script>
<style type="text/css">
.logo{
 padding-top: 20px;
 padding-left: 20px;
 padding-right: 20px;
 padding-bottom: 20px;
 font-size: 28px;
}
.top{
 padding-bottom: 100px;
}
.login{
 line-height: 32px;
}
.content {
 width: 350px;
 margin: 15px auto auto;
 padding: 50px 50px;
 border: 2px solid #ccc;
 border-radius: 10px;
 box-shadow: 0 0 30px #ccc;
}
.bottom{
 padding-top: 100px;
}
.button{
 margin-top: 10px;
 margin-left: 105px;
}
.update {
 margin: 0 auto;
 padding: 0;
 width: 98%;
}

.update td {
 margin: 0;
 height: 30px;
 padding: 5px;
}

.update .name {
 text-align: right;
}

.title_div {
 width: 350px;
}

body {
 background: url("< s : url value = '/images/gray_bg.png'/ >") 0 0
 repeat-y #F6F6F6;
}
</style>
<script type="text/javascript">
 $(function() {
 pilicat.title2div('title2div');
 pilicat.keysubmit('form1', 'submit', true);
 });
</script>
</head>
<body>
 <div class="logo">首都机场空地自动化协同决策系统</div>
 <div class="rounded table">
 <div class="top"></div>
 <div class="content">
 <form id="form1" action="<s:url value='/u/ulogin'/>" method="post">
 <div align="center"><span style="color: red;">${captcha}</span></div>
 <table class="table table-bordered table-striped" style="width: 310px; padding-left: 50px;">
 <tbody>
 <tr class="login">
 <td><span>账 号: </span></td>
 <td><input type="text" id="userName" name="userName"
 class="input rounded" value="" placeholder="账号" /></td>
 </tr>
 <tr class="login">
 <td><span>密 码: </span></td>
 <td><input type="password" id="passWd" name="passWd"
 class="input rounded" value="" placeholder="密码" /></td>
 </tr>
 <tr class="login">
 <td><span>验证码: </span></td>
 <td><input type="text" id="captcha" name="captcha" size="5" maxlength="5" class="input rounded" value="" placeholder="验证码" />
 <img id="captcha" style="cursor: pointer; cursor: hand; margin-top: -5px; margin-right: -10px;" align="middle"
 onclick="this.src='<s:url value='/u/captcha.htm'/>?'+Math.random();"
 src="<s:url value='/u/captcha.htm'/>">
 </td>
 </tr>
 <tr class="login">
 <td colspan="2">
 <a id="submit" class="submit" href="javascript:;" onclick="submitForm();">登录</a>
 </td>
 </tr>
 </tbody>
 </table>
 </form>
 </div>
 <div class="bottom"></div>
 </div>
 <%@ include file="../../common/bottom.jsp"%>
</body>
</html>

解决方案:

把form表单放在一个div里面 ,然后对这个div监听事件$("#id").keydown(function (){});

#*#监听回车事件

document.onkeydown=function() {

if(event.keyCode==13) {

//这里提交你的表单

$('#ff_login').submit();
}
}

#*#页面编写js脚本进行监听。。。

#*#js监听enter事件#*#

把form表单放在一个div里面 ,然后对这个div监听事件$("#id").keydown(function (){});

#*#获取焦点 监听enter#*#

监听整个body的keypress事件,如果是回车键,激发submit按钮的click事件,当然你的click事件中会有相关的数据验证之类的,如果验证不过,不会提交。

希望本文所述对大家jsp程序设计有所帮助。

更多精彩内容其他人还在看

jsp 使用jstl实现翻页实例代码

这篇文章主要介绍了jsp 使用jstl实现翻页实例代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Jsp中的table多表头导出excel文件具体实现

这篇文章主要介绍了Jsp中的table多表头导出excel文件具体实现,有需要的朋友可以参考一下
收藏 0 赞 0 分享

java(jsp)整合discuz同步登录功能详解

jsp整合discuz同步登录功能详解,Uenter是Comsenz旗下各个产品之间信息直接传递的一个桥梁,通过UCenter站长可以无缝整合Comsenz系列产品,Center拥有机制完善的接口,经过简单修改便可以挂接其它任何平台的第三方的网络应用程序
收藏 0 赞 0 分享

jsp页面传参乱码的解决方法

本篇文章主要是对jsp页面传参乱码的解决方法进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助
收藏 0 赞 0 分享

jsp分页显示的实现代码

这篇文章主要介绍了jsp分页显示的实现代码,有需要的朋友可以参考一下
收藏 0 赞 0 分享

Linux和Windows中tomcat修改内存大小的方法

Linux和Windows中tomcat修改内存大小的方法,可以利用JVM提供的-Xmn -Xms -Xmx等选项可进行设置,大家参考使用吧
收藏 0 赞 0 分享

使用maven+eclipse搭建struts2开发环境

Struts 2是Apache基金会的明星级产品,提供了对MVC的一个清晰的实现,下面就为大家介绍一下使用maven+eclipse搭建struts2开发环境的方法
收藏 0 赞 0 分享

jsp网页计数器实现示例

网页计数器想必大家都有见到过吧,记录每一个访问者,下面有个不错的示例,感兴趣的朋友可以参考下
收藏 0 赞 0 分享

jsp页面间传中文参数示例(页面传参数编码)

在url地址栏使用中文传参数可能会是乱码了,下面我们来看看正确的jsp中页面间传中文参数转码的方法
收藏 0 赞 0 分享

servlet分页代码示例

本文介绍了servlet分页代码实现,采用Oracle数据库,获取SCOTT用户EMP表中的数据,分页实现步骤看下面代码
收藏 0 赞 0 分享
查看更多