说说回车键触发表单提交的问题

我们有时候希望回车键敲在文本框(input element)里来提交表单(form),但有时候又不希望如此。比如搜索行为,希望输入完关键词之后直接按回车键立即提交表单,而有些复杂表单,可能要避免回车键误操作在未完成表单填写的时候就触发了表单提交。

要控制这些行为,不需要借助JS,浏览器已经帮我们做了这些处理,这里总结几条规则:

1、如果表单里有一个type=”submit”的按钮,回车键生效。

2、如果表单里只有一个type=”text”的input,不管按钮是什么type,回车键生效。

3、如果按钮不是用input,而是用button,并且没有加type,IE下默认为type=button,FX默认为type=submit。

4、其他表单元素如textarea、select不影响,radio checkbox不影响触发规则,但本身在FX下会响应回车键,在IE下不响应。

5、type=”image”的input,效果等同于type=”submit”,不知道为什么会设计这样一种type,不推荐使用,应该用CSS添加背景图合适些。

实际应用的时候,要让表单响应回车键很容易,保证表单里有个type=”submit”的按钮就行。而当只有一个文本框又不希望响应回车键怎么办呢?我的方法有点别扭,就是再写一个无意义的文本框,隐藏起来。根据第3条规则,我们在用button的时候,尽量显式声明type以使浏览器表现一致。

做了一个demo列出了一些例子:

 

ExpandedBlockStart.gif View Code
 1  <! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd" >
 2  < html >
 3  < head >
 4      < meta  http-equiv ="Content-Type"  content ="text/html; charset=GBK" >
 5      < title >submit例子 - aspxhome.com </ title >
 6  </ head >
 7  < body >
 8      < h1 >本demo演示在文本框中按enter键是否触发提交表单 </ h1 >
 9      < h2 >默认情况下,一个文本框的时候,提交,不管按钮type是submit还是button </ h2 >
10      < form  action ="http://www.aspxhome.com" >
11          < input  type ="text" >
12          < input  type ="button"  value ="提交" >
13      </ form >
14      < h2 >一个文本框的时候怎么才能做到不提交,方法是加一个隐藏掉的文本框 </ h2 >
15      < form  action ="http://www.aspxhome.com" >
16          < input  type ="text" >
17          < input  type ="text"  style ="display:none" >
18          < input  type ="button"  value ="提交" >
19      </ form >
20      < h2 >只要有type为submit的按钮存在,一个文本框还是多个文本框都提交 </ h2 >
21      < form  action ="http://www.aspxhome.com" >
22          < input  type ="text" >
23          < input  type ="submit"  value ="提交" >
24      </ form >
25      < h2 >只要有type为submit的按钮存在,一个文本框还是多个文本框都提交 </ h2 >
26      < form  action ="http://www.aspxhome.com" >
27          < input  type ="text" >
28          < input  type ="text" >
29          < input  type ="submit"  value ="提交" >
30      </ form >
31      < h2 >多个文本框的时候,不提交,用type为button的按钮就行啦 </ h2 >
32      < form  action ="http://www.aspxhome.com" >
33          < input  type ="text" >
34          < input  type ="text" >
35          < input  type ="button"  value ="提交" >
36      </ form >
37      < h2 >用button元素时,FX和IE下有不同的表现 </ h2 >
38      < form  action ="http://www.aspxhome.com" >
39          < input  type ="text" >
40          < input  type ="text" >
41          < button >提交 </ button >
42      </ form >
43      < h2 >radio和checkbox在FX下也会触发提交表单,在IE下不会 </ h2 >
44      < form  action ="http://www.aspxhome.com" >
45          < input  type ="text" >
46          < input  type ="radio"  name ="a" >
47          < input  type ="checkbox"  name ="b" >
48          < input  type ="checkbox"  name ="c" >
49          < input  type ="button"  value ="提交" >
50      </ form >
51      < h2 >type为image的按钮,等同于type为submit的效果 </ h2 >
52      < form  action ="http://www.aspxhome.com" >
53          < input  type ="text" >
54          < input  type ="text" >
55          < input  type ="image"  src ="/images/logo.gif" >
56      </ form >
57  </ body >
58  </ html >

 

转载于:https://www.cnblogs.com/cxd4321/archive/2012/05/07/2486926.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值