<input> | HTML输入框标签的定义与用法


定义与用法

  • <input>标签定义HTML文档中的表单输入控件,让用户与网站之间建立数据交互。
  • <input> 标签经常与 <form>标签搭配使用,其type 属性可以改变输入字段类型。
  • 可以使用 <label> 标签来定义 <input> 元素的标题或声明。

示例代码

<form action="#">  
姓名: <input type="text" name="Name"  placeholder="请输入姓名..."><br>  
性别: <input type="text" name="sex" placeholder="请输入性别..."><br>  
<input type="submit" value="提交">  
</form>  

效果展示:

姓名:
性别:

HTML 与 XHTML 之间的差异

  • 在 HTML 中,<input> 标签没有结束标签。
  • 在 XHTML 中,<input> 标签必须被正确地关闭。

HTML 4.01 与 HTML5之间的差异

  • 在 HTML 4.01 中, “align” 数据已经不再使用。HTML5 中不支持该属性。 可以使用CSS来定义 <input> 元素的对齐方式。
  • 在 HTML5中, <input> 添加了几个属性,并且添加了对应的值。

浏览器支持

目前大多数浏览器支持 <input>标签。


标签属性

: HTML5新标签。

属性描述
acceptaudio/* video/* image/* MIME_type规定通过文件上传来提交的文件的类型。 (只针对type=”file”)
alignleft right top middle bottomHTML5已废弃,不赞成使用。规定图像输入的对齐方式。 (只针对type=”image”)
alttext定义图像输入的替代文本。 (只针对type=”image”)
autocompleteon offautocomplete 属性规定 <input> 元素输入字段是否应该启用自动完成功能。
autofocusautofocus属性规定当页面加载时 <input> 元素应该自动获得焦点。
checkedcheckedchecked 属性规定在页面加载时应该被预先选定的 <input> 元素。 (只针对 type=”checkbox” 或者 type=”radio”)
disableddisableddisabled 属性规定应该禁用的 <input> 元素。
formform_idform 属性规定 <input> 元素所属的一个或多个表单。
formactionURL属性规定当表单提交时处理输入控件的文件的 URL。(只针对 type=”submit” 和 type=”image”)
formenctypeapplication/x-www-form-urlencoded multipart/form-data text/plain属性规定当表单数据提交到服务器时如何编码(只适合 type=”submit” 和 type=”image”)。
formmethodget post定义发送表单数据到 action URL 的 HTTP 方法。 (只适合 type=”submit” 和 type=”image”)
formnovalidateformnovalidateformnovalidate 属性覆盖 <form> 元素的 novalidate 属性。
formtarget_blank _self _parent _top framename规定表示提交表单后在哪里显示接收到响应的名称或关键词。(只适合 type=”submit” 和 type=”image”)
heightpixels规定 <input>元素的高度。(只针对type=”image”)
listdatalist_id属性引用 <datalist> 元素,其中包含 <input> 元素的预定义选项。
maxnumber date属性规定 <input> 元素的最大值。
maxlengthnumber属性规定 <input> 元素中允许的最大字符数。
minnumber date属性规定 <input>元素的最小值。
multiplemultiple属性规定允许用户输入到 <input> 元素的多个值。
nametextname 属性规定 <input> 元素的名称。
patternregexppattern 属性规定用于验证 <input> 元素的值的正则表达式。
placeholdertextplaceholder 属性规定可描述输入 <input> 字段预期值的简短的提示信息 。
readonlyreadonlyreadonly 属性规定输入字段是只读的。
requiredrequired属性规定必需在提交表单之前填写输入字段。
sizenumbersize 属性规定以字符数计的 <input> 元素的可见宽度。
srcURLsrc 属性规定显示为提交按钮的图像的 URL。 (只针对 type=”image”)
stepnumberstep 属性规定 <input> 元素的合法数字间隔。
typebutton
checkbox
color
date
datetime
datetime-local
email
file
hidden
image
month
number
password
radio
range
reset
search
submit
tel
text
time
url
week
type 属性规定要显示的 <input> 元素的类型。
valuetext指定 <input> 元素 value 的值。
widthpixelswidth 属性规定 <input> 元素的宽度。 (只针对type=”image”)

全局属性

<input> 标签支持全局属性,查看完整属性表 HTML全局属性。


事件属性

<input> 标签支持所有 HTML事件属性。


相关