全部学科
Python全栈
python
NodeJS全栈
nodejs
小程序首页
📅 2026-05-17 8 分钟 ✍️ juanwangdev

语义化表单

语义化表单通过使用正确的标签和属性,帮助浏览器、辅助技术理解表单内容和关系。

核心结构

表单容器

HTML
<form action="/submit" method="POST" novalidate>
  <!-- 表单内容 -->
</form>

表单分组

HTML
<form>
  <fieldset>
    <legend>个人信息</legend>
    <!-- 相关输入项 -->
  </fieldset>

  <fieldset>
    <legend>联系方式</legend>
    <!-- 相关输入项 -->
  </fieldset>
</form>

标签关联

label 元素

HTML
<!-- 显式关联(推荐) -->
<label for="username">用户名</label>
<input type="text" id="username" name="username">

<!-- 隐式关联 -->
<label>
  用户名
  <input type="text" name="username">
</label>

关联方式对比

方式语法特点
显式label for="id" + input id可灵活布局
隐式label 包裹 input结构紧凑

输入类型语义

常用类型

HTML
<!-- 文本类 -->
<input type="text" placeholder="请输入">
<input type="password" autocomplete="current-password">
<input type="email" autocomplete="email">
<input type="tel" pattern="[0-9]{11}">
<input type="url" placeholder="https://">

<!-- 数值类 -->
<input type="number" min="0" max="100" step="1">
<input type="range" min="0" max="100">

<!-- 时间类 -->
<input type="date">
<input type="time">
<input type="datetime-local">

<!-- 其他 -->
<input type="color">
<input type="search" aria-label="搜索">
<input type="file" accept=".pdf,.doc">

类型选择表

类型用途移动端优化
email邮箱显示邮箱键盘
tel电话显示数字键盘
url网址显示网址键盘
number数值显示数字键盘
date日期显示日期选择器

表单属性

验证属性

HTML
<input type="text" required minlength="2" maxlength="20">
<input type="email" required>
<input type="number" min="1" max="10">
<input type="text" pattern="[A-Za-z]{3}">
属性说明
required必填字段
minlength/maxlength文本长度限制
min/max数值范围限制
pattern正则验证模式

自动填充

HTML
<input type="text" name="name" autocomplete="name">
<input type="email" autocomplete="email">
<input type="tel" autocomplete="tel">
<input type="address" autocomplete="street-address">

辅助提示

helpertext 模式

HTML
<div class="form-group">
  <label for="password">密码</label>
  <input
    type="password"
    id="password"
    aria-describedby="password-hint"
  >
  <small id="password-hint">密码需至少8位,包含字母和数字</small>
</div>

错误提示

HTML
<div class="form-group">
  <label for="email">邮箱</label>
  <input
    type="email"
    id="email"
    aria-invalid="true"
    aria-describedby="email-error"
  >
  <span id="email-error" role="alert">请输入有效的邮箱地址</span>
</div>

完整示例

HTML
<form action="/register" method="POST">
  <fieldset>
    <legend>注册账户</legend>

    <div class="form-group">
      <label for="username">用户名 <span aria-hidden="true">*</span></label>
      <input
        type="text"
        id="username"
        name="username"
        required
        minlength="3"
        maxlength="20"
        aria-required="true"
        autocomplete="username"
      >
    </div>

    <div class="form-group">
      <label for="email">邮箱 <span aria-hidden="true">*</span></label>
      <input
        type="email"
        id="email"
        name="email"
        required
        aria-required="true"
        autocomplete="email"
      >
    </div>

    <div class="form-group">
      <label for="password">密码</label>
      <input
        type="password"
        id="password"
        name="password"
        required
        minlength="8"
        aria-describedby="password-hint"
      >
      <small id="password-hint">至少8位,建议包含字母、数字、符号</small>
    </div>
  </fieldset>

  <button type="submit">注册</button>
</form>

注意:每个表单控件必须有对应的 label,placeholder 不能替代 label。

要点总结

  • 使用 <fieldset> + <legend> 分组相关表单项
  • <label> 必须与 <input> 关联(for/id 或隐式包裹)
  • 选择正确的 input type,触发移动端优化键盘
  • 验证属性:required、minlength、maxlength、pattern
  • 错误提示使用 aria-invalid + aria-describedby

📝 发现内容有误?点击此处直接编辑

← 上一篇 语义化导航
下一篇 → 语义化表格
想查看更多题目和详细解析?
小程序提供完整的题库、模拟考试和详细解析
马上就来

长按或扫描二维码,立即体验

扫码体验小程序
马上就来
使用微信扫描二维码
立即体验完整题库