input 标签总览
<input> 是 HTML 表单中最核心、最灵活的元素。通过 type 属性,单个 <input> 标签可以呈现 20 多种不同的控件形态——从文本框、密码框到日期选择器、颜色选择器、文件选择器等。本节将全面梳理 <input> 的所有 type 值和全局属性,为后续各类型的详细学习建立整体认知。
前置知识
阅读本节前,建议先了解:autocomplete 与 novalidate
基础概念
input 的核心地位
<input> 是一个自闭合(void)元素,没有子内容,所有行为和外观完全由属性决定。它是 Web 表单中使用频率最高的元素,几乎所有用户数据输入都通过 <input> 完成。
<!-- 不同 type 产生完全不同的控件 -->
<input type="text"> <!-- 文本输入框 -->
<input type="password"> <!-- 密码输入框 -->
<input type="checkbox"> <!-- 复选框 -->
<input type="radio"> <!-- 单选按钮 -->
<input type="file"> <!-- 文件选择器 -->
<input type="date"> <!-- 日期选择器 -->type 决定一切
type 属性是 <input> 最关键的属性,它决定了:
- 控件的外观:渲染为文本框、按钮、滑块还是下拉选择器
- 输入方式:键盘输入、点击选择、拖拽还是文件选取
- 数据类型:文本、数字、日期、颜色、文件
- 浏览器验证:是否自动验证输入格式
- 移动端键盘:弹出哪种虚拟键盘
- 辅助功能:屏幕阅读器如何描述该控件
语法
input 基本语法
<input
type="text"
name="username"
id="username"
value="默认值"
placeholder="提示文字"
required
disabled
readonly
autocomplete="username"
maxlength="20"
>全部 type 值一览
HTML5 定义了以下 <input> type 值:
| type 值 | 控件类型 | 数据类型 | 浏览器自动验证 | 移动端键盘 |
|---|---|---|---|---|
text | 文本输入框 | 字符串 | 否 | 默认键盘 |
password | 密码输入框 | 字符串(隐藏) | 否 | 默认键盘 |
email | 邮箱输入框 | 邮箱地址 | 是 | 邮箱键盘 |
number | 数字输入框 | 数字 | 是 | 数字键盘 |
tel | 电话输入框 | 电话号码 | 否 | 电话键盘 |
url | URL 输入框 | 网址 | 是 | URL 键盘 |
search | 搜索输入框 | 搜索字符串 | 否 | 搜索键盘 |
hidden | 隐藏字段 | 任意 | 否 | 无 |
checkbox | 复选框 | 布尔/值 | 否 | 无 |
radio | 单选按钮 | 值 | 否 | 无 |
file | 文件选择器 | 文件 | 否 | 无 |
submit | 提交按钮 | 无 | 否 | 无 |
reset | 重置按钮 | 无 | 否 | 无 |
button | 普通按钮 | 无 | 否 | 无 |
image | 图像提交按钮 | 坐标 | 否 | 无 |
color | 颜色选择器 | 颜色值 | 否 | 无 |
date | 日期选择器 | 日期 | 是 | 无(原生控件) |
datetime-local | 日期时间选择器 | 日期时间 | 是 | 无(原生控件) |
month | 月份选择器 | 年-月 | 是 | 无(原生控件) |
week | 周选择器 | 年-周 | 是 | 无(原生控件) |
time | 时间选择器 | 时间 | 是 | 无(原生控件) |
range | 滑块控件 | 数字范围 | 否 | 无 |
datetime | 日期时间(已废弃) | - | - | - |
* | 未知类型 | - | - | 回退为 text |
未知 type 回退
如果 <input> 的 type 设置为浏览器不认识的值,浏览器会将其回退为 type="text" 显示一个普通文本输入框。
详细说明
全局 input 属性
以下属性适用于大多数(但不一定是全部)<input> 类型:
通用属性
| 属性 | 适用类型 | 说明 | 示例值 |
|---|---|---|---|
name | 全部 | 控件名称,提交数据的键名 | "username" |
id | 全部 | 唯一标识符 | "emailInput" |
value | 全部 | 控件的当前值 | "hello" |
disabled | 全部 | 禁用控件,不提交 | - |
form | 全部 | 关联外部表单 | "myForm" |
autofocus | 全部 | 页面加载后自动聚焦 | - |
tabindex | 全部 | Tab 键导航顺序 | "1" |
title | 全部 | 悬停提示文本 | "请输入用户名" |
文本输入相关属性
| 属性 | 适用类型 | 说明 | 示例值 |
|---|---|---|---|
placeholder | text, search, url, tel, email, password, number | 空值时显示的占位文字 | "请输入..." |
maxlength | text, search, url, tel, email, password | 最大字符数 | "100" |
minlength | text, search, url, tel, email, password | 最小字符数(验证用) | "2" |
pattern | text, search, url, tel, email, password | 正则验证模式 | "^[a-z]+$" |
size | text, search, url, tel, email, password | 可见字符宽度 | "20" |
readonly | text, search, url, tel, email, password, date, time 等 | 只读,可提交 | - |
spellcheck | text, search, url, tel, email | 拼写检查 | "true" / "false" |
list | text, search, url, tel, email, number, date, time 等 | 关联的 datalist id | "suggestions" |
multiple | email, file | 允许多个值/文件 | - |
dirname | text, search | 发送文本方向 | "dir" |
数字相关属性
| 属性 | 适用类型 | 说明 | 示例值 |
|---|---|---|---|
min | number, date, datetime-local, month, week, time, range | 最小值 | "0" |
max | number, date, datetime-local, month, week, time, range | 最大值 | "100" |
step | number, date, datetime-local, month, week, time, range | 步进值 | "1" |
验证相关属性
| 属性 | 适用类型 | 说明 |
|---|---|---|
required | text, search, url, tel, email, password, number, date, time, checkbox, radio, file | 必填 |
autocomplete | text, search, url, tel, email, password, number, date, time | 自动补全类型 |
按 type 分类详解
文本类输入
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>文本类输入控件</title>
<style>
.demo-group {
margin-bottom: 20px;
padding: 15px;
border: 1px solid #e0e0e0;
border-radius: 8px;
}
.demo-group h3 {
margin-top: 0;
color: #333;
}
label {
display: inline-block;
width: 120px;
font-weight: 500;
}
input {
padding: 8px;
border: 1px solid #ccc;
border-radius: 4px;
margin: 4px 0;
}
</style>
</head>
<body>
<h2>文本类输入控件一览</h2>
<div class="demo-group">
<h3>type="text"(文本)</h3>
<label>用户名:</label>
<input type="text" name="username" placeholder="请输入用户名">
</div>
<div class="demo-group">
<h3>type="password"(密码)</h3>
<label>密码:</label>
<input type="password" name="password" placeholder="请输入密码">
</div>
<div class="demo-group">
<h3>type="email"(邮箱)</h3>
<label>邮箱:</label>
<input type="email" name="email" placeholder="user@example.com">
</div>
<div class="demo-group">
<h3>type="number"(数字)</h3>
<label>数量:</label>
<input type="number" name="quantity" min="1" max="100" step="1" value="1">
</div>
<div class="demo-group">
<h3>type="tel"(电话)</h3>
<label>电话:</label>
<input type="tel" name="phone" placeholder="13800138000">
</div>
<div class="demo-group">
<h3>type="url"(网址)</h3>
<label>网站:</label>
<input type="url" name="website" placeholder="https://example.com">
</div>
<div class="demo-group">
<h3>type="search"(搜索)</h3>
<label>搜索:</label>
<input type="search" name="q" placeholder="搜索...">
</div>
</body>
</html>选择类控件
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>选择类控件</title>
</head>
<body>
<h2>选择类控件</h2>
<h3>checkbox(复选框)</h3>
<label><input type="checkbox" name="hobby" value="reading"> 阅读</label>
<label><input type="checkbox" name="hobby" value="music"> 音乐</label>
<label><input type="checkbox" name="hobby" value="sports" checked> 运动</label>
<h3>radio(单选按钮)</h3>
<label><input type="radio" name="gender" value="male"> 男</label>
<label><input type="radio" name="gender" value="female" checked> 女</label>
<h3>color(颜色选择器)</h3>
<label>选择颜色:<input type="color" name="theme" value="#4a90d9"></label>
<h3>range(滑块)</h3>
<label>音量:<input type="range" name="volume" min="0" max="100" value="50"></label>
</body>
</html>日期时间类控件
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>日期时间类控件</title>
</head>
<body>
<h2>日期时间类控件</h2>
<h3>date(日期)</h3>
<input type="date" name="birthday">
<h3>time(时间)</h3>
<input type="time" name="alarm">
<h3>datetime-local(日期时间)</h3>
<input type="datetime-local" name="meeting">
<h3>month(月份)</h3>
<input type="month" name="start-month">
<h3>week(周)</h3>
<input type="week" name="work-week">
</body>
</html>按钮类控件
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>按钮类控件</title>
</head>
<body>
<h2>按钮类控件</h2>
<!-- submit:提交表单(默认) -->
<input type="submit" value="提交表单">
<!-- reset:重置表单 -->
<input type="reset" value="重置表单">
<!-- button:普通按钮(不做任何事) -->
<input type="button" value="普通按钮" onclick="alert('点击了')">
<!-- image:图像提交按钮 -->
<input type="image" src="submit.png" alt="提交" width="100" height="40">
<!-- hidden:隐藏字段(不可见,数据随表单提交) -->
<input type="hidden" name="token" value="abc123">
</body>
</html>文件类控件
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>文件控件</title>
</head>
<body>
<h2>文件选择控件</h2>
<!-- 单文件选择 -->
<input type="file" name="avatar" accept="image/*">
<!-- 多文件选择 -->
<input type="file" name="documents" multiple accept=".pdf,.doc,.docx">
</body>
</html>移动端键盘类型
在移动设备上,不同的 type 值会触发不同类型的虚拟键盘,这对用户体验至关重要。
| type 值 | 移动端键盘类型 | 特点 |
|---|---|---|
text | 标准键盘 | 完整的字母和数字键盘 |
email | 邮箱键盘 | 包含 @ 和 . 快捷键 |
tel | 电话键盘 | 数字键盘 + * # |
url | URL 键盘 | 包含 / . .com 快捷键 |
number | 数字键盘 | 纯数字键盘 |
search | 搜索键盘 | 标准键盘 + "搜索"按钮 |
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>移动端键盘优化</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h2>移动端表单优化</h2>
<!--
在手机上打开此页面
点击不同输入框会弹出不同的键盘
-->
<!-- 弹出带 @ 和 .com 的邮箱键盘 -->
<input type="email" placeholder="邮箱(邮箱键盘)">
<!-- 弹出纯数字电话键盘 -->
<input type="tel" placeholder="电话(电话键盘)">
<!-- 弹出带 / 和 .com 的 URL 键盘 -->
<input type="url" placeholder="网址(URL键盘)">
<!-- 弹出纯数字键盘 -->
<input type="number" placeholder="数量(数字键盘)">
<!-- 弹出标准键盘 + 搜索按钮 -->
<input type="search" placeholder="搜索(搜索键盘)">
</body>
</html>实战示例
用户信息采集表单
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>input 类型综合示例</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
max-width: 600px;
margin: 30px auto;
padding: 20px;
}
.form-card {
background: white;
padding: 30px;
border-radius: 12px;
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}
h2 {
text-align: center;
margin-bottom: 24px;
}
.form-group {
margin-bottom: 16px;
}
label {
display: block;
margin-bottom: 6px;
font-weight: 500;
}
input {
width: 100%;
padding: 10px 12px;
border: 1px solid #ddd;
border-radius: 6px;
box-sizing: border-box;
font-size: 14px;
}
input:focus {
outline: none;
border-color: #4a90d9;
}
.row {
display: flex;
gap: 16px;
}
.row .form-group {
flex: 1;
}
button {
width: 100%;
padding: 12px;
background-color: #4a90d9;
color: white;
border: none;
border-radius: 6px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<body>
<div class="form-card">
<h2>用户信息</h2>
<form action="/api/profile" method="post">
<!-- 隐藏字段 -->
<input type="hidden" name="source" value="web">
<div class="row">
<div class="form-group">
<label for="name">姓名</label>
<input type="text" id="name" name="name"
placeholder="真实姓名" required maxlength="20">
</div>
<div class="form-group">
<label for="birthday">生日</label>
<input type="date" id="birthday" name="birthday">
</div>
</div>
<div class="form-group">
<label for="email">邮箱</label>
<input type="email" id="email" name="email"
placeholder="user@example.com" required>
</div>
<div class="form-group">
<label for="phone">手机号</label>
<input type="tel" id="phone" name="phone"
placeholder="13800138000" pattern="^1[3-9]\d{9}$">
</div>
<div class="form-group">
<label for="website">个人网站</label>
<input type="url" id="website" name="website"
placeholder="https://yoursite.com">
</div>
<div class="row">
<div class="form-group">
<label for="city">所在城市</label>
<input type="text" id="city" name="city"
placeholder="如:北京" list="cityList">
<datalist id="cityList">
<option value="北京">
<option value="上海">
<option value="广州">
<option value="深圳">
<option value="杭州">
</datalist>
</div>
<div class="form-group">
<label for="theme">主题颜色</label>
<input type="color" id="theme" name="theme" value="#4a90d9">
</div>
</div>
<div class="form-group">
<label for="satisfaction">满意度:<span id="satisfactionValue">50</span>%</label>
<input type="range" id="satisfaction" name="satisfaction"
min="0" max="100" value="50">
</div>
<div class="form-group">
<label for="avatar">头像</label>
<input type="file" id="avatar" name="avatar" accept="image/*">
</div>
<button type="submit">保存信息</button>
</form>
</div>
<script>
// 实时显示滑块值
const range = document.getElementById('satisfaction');
const display = document.getElementById('satisfactionValue');
range.addEventListener('input', () => {
display.textContent = range.value;
});
</script>
</body>
</html>注意事项
name 属性不可省略:没有
name的<input>控件不会随表单提交。id用于前端关联,name用于数据提交。type 默认值是 text:如果省略
type属性,<input>默认为type="text"。datetime 类型已废弃:
type="datetime"在 HTML5 中已被废弃,使用datetime-local代替。日期时间类控件的浏览器兼容性:
date、time、datetime-local等原生日期控件在 Safari(iOS)上的支持较晚。在生产环境中,可能需要使用第三方日期选择器库作为 polyfill。color 控件的外观差异:
type="color"在不同浏览器中呈现不同(Chrome 为色块,Firefox 为文字按钮),样式控制能力有限。range 控件需要 JavaScript:
type="range"本身只显示滑块,要显示当前值需要配合 JavaScript 监听input事件。
最佳实践
根据数据类型选择正确的 type:使用语义化的 type 值(
email、tel、url、number等),而非全部使用text。这不仅提供自动验证,还优化移动端体验。为所有输入框关联 label:使用
<label for="id">或将<input>放在<label>内部,提高可访问性。合理使用 placeholder:placeholder 是辅助提示,不能替代 label。不要把 placeholder 当作唯一的标签说明。
为敏感字段使用 autocomplete token:如
autocomplete="current-password"、autocomplete="cc-number"等。使用 hidden 传递元数据:
type="hidden"适合传递用户不可见的辅助数据(如 token、来源标识等),但不要用于安全相关的数据。
下一节
继续学习:text 文本输入