HTML 表单与输入
BeginnerHTML Interaction表单是用户与网站交互的主要方式——登录、注册、搜索、提交数据都依赖表单元素。
一、📝 表单基本结构
html
<form action="/submit" method="POST">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" />
<label for="email">邮箱:</label>
<input type="email" id="email" name="email" />
<button type="submit">提交</button>
</form>核心属性
| 属性 | 说明 |
|---|---|
action | 表单提交的目标 URL(服务器端处理地址) |
method | HTTP 方法:GET(查询)或 POST(提交数据) |
GET vs POST 表单
| 特性 | method="GET" | method="POST" |
|---|---|---|
| 数据位置 | URL 参数(?key=value) | 请求体(Body) |
| 可见性 | URL 中可见 | URL 中不可见 |
| 数据量 | 受 URL 长度限制(约 2KB) | 无明显限制 |
| 适用场景 | 搜索、筛选 | 登录、注册、文件上传 |
二、🔤 输入元素(Input Types)
<input> 是最灵活的表单元素,通过 type 属性可变身为多种控件:
2.1 文本类
| type | 渲染效果 | 用途 |
|---|---|---|
text | 单行文本框 | 通用文本输入 |
password | 密码框(内容隐藏) | 密码输入 |
email | 文本框 + 邮箱格式验证 | 电子邮件 |
url | 文本框 + URL 格式验证 | 网址输入 |
tel | 文本框(移动端弹出数字键盘) | 电话号码 |
search | 搜索框(可能带清除按钮) | 站内搜索 |
2.2 数值与日期
| type | 渲染效果 | 用途 |
|---|---|---|
number | 数字输入框(带上下箭头) | 数量、金额 |
range | 滑块 | 音量、亮度 |
date | 日期选择器 | 出生日期 |
time | 时间选择器 | 预约时间 |
datetime-local | 日期+时间选择器 | 活动时间 |
month | 月份选择器 | 信用卡过期月 |
color | 颜色选择器 | 主题颜色 |
2.3 选择与开关
| type | 渲染效果 | 用途 |
|---|---|---|
checkbox | 复选框 ☑ | 多选(如同意条款) |
radio | 单选按钮 ◉ | 单选(如性别) |
html
<!-- 单选按钮:同一组使用相同 name -->
<input type="radio" id="male" name="gender" value="male" />
<label for="male">男</label>
<input type="radio" id="female" name="gender" value="female" />
<label for="female">女</label>2.4 文件与隐藏
| type | 渲染效果 | 用途 |
|---|---|---|
file | 文件选择按钮 | 上传文件 |
hidden | 不可见 | 传递隐藏数据(如 CSRF 令牌) |
2.5 按钮类
| type | 作用 |
|---|---|
submit | 提交表单 |
reset | 重置所有表单字段 |
button | 普通按钮(需 JS 绑定行为) |
三、📋 其他表单元素
3.1 下拉选择(Select)
html
<label for="city">城市:</label>
<select id="city" name="city">
<option value="">请选择</option>
<option value="beijing">北京</option>
<option value="shanghai">上海</option>
<option value="guangzhou">广州</option>
</select>使用 <optgroup> 可以为选项分组:
html
<select name="food">
<optgroup label="水果">
<option value="apple">苹果</option>
<option value="banana">香蕉</option>
</optgroup>
<optgroup label="蔬菜">
<option value="carrot">胡萝卜</option>
<option value="tomato">番茄</option>
</optgroup>
</select>3.2 多行文本(Textarea)
html
<label for="bio">自我介绍:</label>
<textarea id="bio" name="bio" rows="5" cols="40"
placeholder="请输入自我介绍..."></textarea>| 属性 | 说明 |
|---|---|
rows | 可见行数(高度) |
cols | 可见列数(宽度) |
placeholder | 占位提示文本 |
3.3 字段集(Fieldset)
将相关的表单控件分组,提升结构和可读性:
html
<fieldset>
<legend>个人信息</legend>
<label for="name">姓名:</label>
<input type="text" id="name" name="name" />
<label for="age">年龄:</label>
<input type="number" id="age" name="age" />
</fieldset>四、✅ 表单验证
HTML5 内置了客户端验证能力,无需 JavaScript 即可实现基本校验。
4.1 验证属性
| 属性 | 说明 | 示例 |
|---|---|---|
required | 字段必填 | <input required /> |
minlength | 最小字符数 | <input minlength="6" /> |
maxlength | 最大字符数 | <input maxlength="20" /> |
min / max | 数值范围 | <input type="number" min="0" max="100" /> |
pattern | 正则表达式匹配 | <input pattern="[A-Za-z]{3}" /> |
step | 数值步进 | <input type="number" step="0.01" /> |
4.2 示例:注册表单
html
<form action="/register" method="POST">
<label for="username">用户名(3-20 字符):</label>
<input type="text" id="username" name="username"
required minlength="3" maxlength="20" />
<label for="email">邮箱:</label>
<input type="email" id="email" name="email" required />
<label for="password">密码(至少 8 位):</label>
<input type="password" id="password" name="password"
required minlength="8" />
<label for="age">年龄(18-120):</label>
<input type="number" id="age" name="age"
min="18" max="120" />
<button type="submit">注册</button>
</form>4.3 类型自带验证
某些 type 自带格式验证,无需额外属性:
| type | 自动验证 |
|---|---|
email | 必须包含 @ 和域名 |
url | 必须是有效的 URL 格式 |
number | 必须是数字 |
date | 必须是有效日期 |
不要仅依赖客户端验证
HTML5 验证可以被浏览器开发者工具轻松绕过。服务器端验证 是安全的最后一道防线,永远不可省略。
五、♿ 无障碍最佳实践
5.1 Label 关联
每个输入控件都必须关联一个 <label>,有两种方式:
html
<!-- 方式一:for + id 关联(推荐) -->
<label for="email">邮箱:</label>
<input type="email" id="email" name="email" />
<!-- 方式二:嵌套关联 -->
<label>
邮箱:
<input type="email" name="email" />
</label>关联后点击 label 文本也能聚焦输入框,提升可用性和无障碍体验。
5.2 Placeholder 不能替代 Label
html
<!-- ❌ 错误:只有 placeholder,没有 label -->
<input type="text" placeholder="请输入用户名" />
<!-- ✅ 正确:label + placeholder -->
<label for="user">用户名:</label>
<input type="text" id="user" placeholder="例如:john_doe" />
placeholder在用户开始输入后会消失,不能作为字段说明的唯一来源。
六、🏷️ 常用 Input 属性速查
| 属性 | 说明 |
|---|---|
name | 字段名(提交时作为键名) |
value | 默认值 |
placeholder | 占位提示文本 |
required | 必填 |
disabled | 禁用(不可编辑、不提交) |
readonly | 只读(不可编辑、但会提交) |
autofocus | 页面加载时自动聚焦 |
autocomplete | 浏览器自动填充(on / off) |