如何实现点击回车提交表单

如何实现点击回车提交表单:
在默认情况下,点击回车是不能够提交表单的,有时候感觉可能点击回车能够提交表单更为方便,下面就通过一段实例简单介绍一下如何实现此效果,希望对需要的朋友有所帮助,代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>点击回车提交表单-蚂蚁部落</title> 
<style type="text/css">
ul{
  list-style:none;
}
</style>
<script type="text/javascript" src="mytest/jQuery/jquery-1.8.3.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("#theform").keypress(function(e){ 
    if(e.which==13) { 
      $("#theform").submit(); 
    } 
  })
  $(window).keydown(function(e){ 
    if(e.which==13){ 
      return false; 
    } 
  }) 
})
</script>
</head>
<body>
<form action="http://www.softwhy.com" method="post" id="theform">
<ul>
<li>姓名:<input type="text" name="username" /></li>
<li>密码:<input type="text" name="username" /></li>
<li><input type="submit" value="提交" /><input type="reset" value="重置" /></li>
</ul>
</form>
</body>
</html>

 以上代码,在点击回车之后能够提交表单,当然在以上代码中没有进行表单验证,这个需要自行扩充。

代码比较简单,这里就不做介绍了,可以参阅相关阅读内容,如有任何问题可以留言跟帖。
相关阅读:
1.e.which可以参阅jQuery的event.which属性一章节。 
2.keypress事件可以参阅jQuery的keypress事件一章节。
3.keydown事件可以参阅jQuery的keydown事件一章节。

原文地址是:http://www.softwhy.com/forum.php?mod=viewthread&tid=8768

更多内容可以参阅:http://www.softwhy.com/jquery/

猜你喜欢

转载自softwhy.iteye.com/blog/2266574