原文链接:http://www.nowamagic.net/html/html_AboutInputSummit.php

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

要控制这些行为,不需要借助 JavaScript,浏览器已经帮我们做了这些处理,下面举几个例子来说明:

    1. 默认情况下,一个文本框的时候,提交,不管按钮 type 是 submit 还是 button:
1 <form action="http://www.nowamagic.net">
2     <input type="text">
3     <input type="button" value="提交">
4 </form>
    1. 一个文本框的时候怎么才能做到不提交,方法是加一个隐藏掉的文本框:
1 <form action="http://www.nowamagic.net">
2     <input type="text">
3     <input type="text" style="display:none">
4     <input type="button" value="提交">
5 </form>
    1. 只要有type为submit的按钮存在,一个文本框还是多个文本框都提交:
1 <form action="http://www.nowamagic.net">
2     <input type="text">
3     <input type="submit" value="提交">
4 </form>
1 <form action="http://www.nowamagic.net">
2     <input type="text">
3     <input type="text">
4     <input type="submit" value="提交">
5 </form>
    1. 多个文本框的时候,不提交,用type为button的按钮就行啦:
1 <form action="http://www.nowamagic.net">
2     <input type="text">
3     <input type="text">
4     <input type="button" value="提交">
5 </form>
    1. 用button元素时,FF和IE下有不同的表现:
1 <form action="http://www.nowamagic.net">
2     <input type="text">
3     <input type="text">
4     <button>提交</button>
5 </form>
    1. radio和checkbox在FX下也会触发提交表单,在IE下不会:
1 <form action="http://www.nowamagic.net">
2     <input type="text">
3     <input type="radio" name="a">
4     <input type="checkbox" name="b">
5     <input type="checkbox" name="c">
6     <input type="button" value="提交">
7 </form>
    1. type为image的按钮,等同于type为submit的效果
1 <form action="http://www.nowamagic.net">
2     <input type="text">
3     <input type="text">
4     <input type="image" src="http://www.nowamagic.net/images/FeedMe.jpg">
5 </form>

总结几条规则:

  • 如果表单里有一个type=”submit”的按钮,回车键生效。
  • 如果表单里只有一个type=”text”的input,不管按钮是什么type,回车键生效。
  • 如果按钮不是用input,而是用button,并且没有加type,IE下默认为type=button,FF默认为type=submit。
  • 其他表单元素如textarea、select不影响,radio checkbox不影响触发规则,但本身在FF下会响应回车键,在IE下不响应。
  • type=”image”的input,效果等同于type=”submit”,不知道为什么会设计这样一种type,不推荐使用,应该用CSS添加背景图合适些。

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

【转载】input 中 type='text' 的提交问题的更多相关文章

  1. 给 input 中 type="text" 设置CSS样式

    input[type="text"], input[type="password"] {    border: 1px solid #ccc;    paddi ...

  2. CSS:给 input 中 type="text" 设置CSS样式

    input[type="text"], input[type="password"] {    border: 1px solid #ccc;    paddi ...

  3. 关于<marquee>、<form>、input中的<text>、<password>、<hidden>、<wenbenkuang>、<reset>、<image>、<submit>、<radio>、<checkbox>以及<select><iframe src>的用法

    <html>    <head>        <meta charset="UTF-8">        <title></ ...

  4. 通过样式调整input 中password text默认长度

    原文出处 <input >标签内的type分别为password和text时其默认长度和宽度不一致,而在做登陆框时往往需要将它们的长度和宽度设置一致.如下的方法可以通过css控制使其一致: ...

  5. IE去掉input的type=”text”输入内容时出现的X和type=”password”出现的眼睛图标

    从IE 10开始,type=”text” 的 input 在用户输入内容后,会自动产生一个小叉叉(X),方便用户点击清除已经输入的文本.对于type=”password”的 input 则会在右方显示 ...

  6. IE10去掉input的type=”text”输入内容时出现的小叉号(X)和type=”password”出现的小眼睛图标

    最近在做一个后台管理系统项目,遇到以下两个问题: 1.从IE 10开始,type="text" 的 input 在用户输入内容后,会自动产生一个小叉号(X),方便用户点击清除已经输 ...

  7. 在input中回车后页面提交导致出现HTTP 错误 405.0 - Method Not Allowed

    前些时间在做一个搜索功能时发现一个比較有意思的现象,场景是这种:在一个模态窗体中是一个订单列表.页面的顶部有若干个状态筛选框和一个搜索keyword输入框,当焦点在keyword输入框时按回车,本来是 ...

  8. 【】input中 type=number 去掉箭头

    css中设置: input::-webkit-outer-spin-button, input::-webkit-inner-spin-button { -webkit-appearance: non ...

  9. 关于使用jquery对input中type为radio的标签checked属性的增加与移除

    需求:对radio的checked属性先消除然后进行重新设置: 初步方案: $("auForm input :radio[value='0']").removeAttr('chec ...

随机推荐

  1. 【转】关系映射文件***.hbm.xml详解

    http://blog.sina.com.cn/s/blog_7ffb8dd5010144yo.html 附.Oracle使用标准.可变长度的内部格式来存储数字.这个内部格式精度可以高达38位. NU ...

  2. Oracle Grid Infrastructure: Understanding Split-Brain Node Eviction (文档 ID 1546004.1)

    In this Document   Purpose   Scope   Details   What does "split brain" mean?   Why is this ...

  3. Python安装Selenium3

    概述 2016.10.13,Selenium3.0正式发布,官方说明如下: The major change in Selenium 3.0 is we're removing the origina ...

  4. Appium启动服务报错

    错误信息如下: error: Could not find a device to launch. You requested 'iPhone 6 (8.4)', but the available ...

  5. Objective-C的内存管理

    一.Objective-C内存管理的对象 1. 值类型:比如int.float.struct等基本数据类型. 值类型会被放入栈中,在内存中占有一块连续的内存空间,遵循先进后出的原则,故不会产生碎片. ...

  6. vsftp.conf

    anonymous_enable=NO local_enable=YES write_enable=YES dirmessage_enable=YES xferlog_enable=YES xferl ...

  7. 学习练习 java面向对象梯形面积

    package com.hanqi; public class Ladder { double ShangDi; double XiaDi; double Gao; double MianJi; La ...

  8. 用C++实现网络编程---抓取网络数据包的实现方法

    一般都熟悉sniffer这个工具,它可以捕捉流经本地网卡的所有数据包.抓取网络数据包进行分析有很多用处,如分析网络是否有网络病毒等异常数据,通信协议的分析(数据链路层协议.IP.UDP.TCP.甚至各 ...

  9. C++ Namespace 详解

    命名空间的定义格式为:(取自C++标准文档) named-namespace-definition: namespace identifier { namespace-body } unnamed-n ...

  10. error: could not find library containing RSA_new

    error: could not find library containing RSA_new yum -y install openssl-devel apt-get install libssl ...