HTML <input> 标签
示例
一个带有三个输入字段的 HTML 表单;两个文本字段和一个提交按钮
<form action="/action_page.php">
<label for="fname">名:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">姓氏:</label>
<input type="text" id="lname" name="lname"><br><br>
<input type="submit" value="提交">
</form>
自己动手试一试 »定义和用法
HTML <input>
标签用于指定用户可以输入数据的输入字段。
<input>
元素是最重要的表单元素。
<input>
元素可以显示多种方式,具体取决于 type 属性。
不同的输入类型如下:
<input type="button">
<input type="checkbox">
<input type="color">
<input type="date">
<input type="datetime-local">
<input type="email">
<input type="file">
<input type="hidden">
<input type="image">
<input type="month">
<input type="number">
<input type="password">
<input type="radio">
<input type="range">
<input type="reset">
<input type="search">
<input type="submit">
<input type="tel">
<input type="text">
(默认值)<input type="time">
<input type="url">
<input type="week">
查看 type 属性,了解每种输入类型的示例!
提示和注释
提示:始终使用 <label> 标签为 <input type="text">
、<input type="checkbox">
、<input type="radio">
、<input type="file">
和 <input type="password">
定义标签。
浏览器支持
元素 | |||||
---|---|---|---|---|---|
<input> | 是 | 是 | 是 | 是 | 是 |
属性
Attribute | 值 | 描述 |
---|---|---|
accept | file_extension audio/* video/* image/* media_type |
指定用户在文件选择对话框中可以选择的文件类型的过滤器(仅适用于 type="file") |
alt | text | 为图像指定替代文本(仅适用于 type="image") |
autocomplete | on off |
指定是否启用 <input> 元素的自动完成功能 |
autofocus | autofocus | 指定页面加载时 <input> 元素应自动获得焦点 |
checked | checked | 指定页面加载时 <input> 元素应预先选中(适用于 type="checkbox" 或 type="radio") |
dirname | inputname.dir | 指定将提交文本方向 |
disabled | disabled | 指定 <input> 元素应被禁用 |
form | form_id | 指定 <input> 元素所属的表单 |
formaction | URL | 指定提交表单时表单数据应如何编码(适用于 type="submit" 和 type="image") |
formenctype | application/x-www-form-urlencoded multipart/form-data text/plain |
指定提交表单时表单数据应如何编码(适用于 type="submit" 和 type="image") |
formmethod | get post |
定义提交到 action URL 的数据的 HTTP 方法(适用于 type="submit" 和 type="image") |
formnovalidate | formnovalidate | 定义提交时表单元素不应进行验证 |
formtarget | _blank _self _parent _top framename |
指定提交表单后收到的响应应显示的位置(适用于 type="submit" 和 type="image") |
height | 像素 | <input> 元素的 height(仅适用于 type="image") |
列表 | datalist_id | 引用包含 <input> 元素的预定义选项的 <datalist> 元素 |
max | 数字 date |
指定 <input> 元素的最大值 |
maxlength | 数字 | 指定 <input> 元素允许的最大字符数 |
分钟 | 数字 date |
指定 <input> 元素的最小值 |
minlength | 数字 | 指定 <input> 元素所需的最小字符数 |
multiple | multiple | 指定用户可以在 <input> 元素中输入多个值 |
name | text | 指定 <input> 元素的名称 |
pattern | regexp | 指定 <input> 元素的值要检查的正则表达式 |
placeholder | text | 指定描述 <input> 元素预期值的简短提示 |
popovertarget | element_id | 指定要调用的弹出式元素(仅适用于 type="button") |
popovertargetaction | hide show toggle |
指定点击按钮时弹出式元素会发生什么(仅适用于 type="button") |
readonly | readonly | 指定输入字段是否为只读 |
required | required | 指定在提交表单之前必须填写输入字段 |
大小 | 数字 | <input> 元素的宽度(以字符为单位) |
src | URL | 指定用作提交按钮的图像的 URL(仅适用于 type="image") |
step |
number any |
指定输入字段中合法数字的间隔 |
type | button checkbox color date datetime-local 电子邮件 file hidden 图片 month 数字 password 收音机 range reset search submit tel text time url week |
指定要显示的 <input> 元素类型 |
value | text | 指定 <input> 元素的值 |
width | 像素 | <input> 元素的 width(仅适用于 type="image") |
全局属性
<input>
标签也支持 HTML 中的全局属性。
事件属性
<input>
标签也支持 HTML 中的事件属性。
相关页面
HTML 教程
HTML DOM 参考
- Input Button 对象
- Input Checkbox 对象
- Input Color 对象
- Input Date 对象
- Input Datetime 对象
- Input DatetimeLocal 对象
- Input Email 对象
- Input FileUpload 对象
- Input Hidden 对象
- Input Image 对象
- Input Month 对象
- Input Number 对象
- Input Password 对象
- Input Range 对象
- Input Radio 对象
- Input Reset 对象
- Input Search 对象
- Input Submit 对象
- Input Text 对象
- Input Time 对象
- Input URL 对象
- Input Week 对象
默认 CSS 设置
无。