Skip to content

HTML 表单

构建 HTML 表单使用输入类型, validation, and submission 到 collect 用户数据.

你将学到

  • 如何 结构 HTML 表单使用 <表单> 元素
  • 常见输入类型: 文本, email, 密码, 数字, checkbox, radio
  • 构建-in HTML validation 使用必需, 最小值, 最大值, and 模式
  • 如何 <label> 元素连接 labels 到输入 for accessibility

概念讲解

HTML Forms

Forms are how web pages collect input from users. A login form, a search box, a contact page, a shopping cart checkout — all are forms. HTML provides a rich set of form elements and input types so you can build these without any JavaScript, though JS enhances them later.

The Form Element

每个form starts with a <表单> element. Two attributes matter most: 操作 (the URL the form submits to) and 方法 (GET or POST). GET appends data to the URL; POST sends it in the request body. Use POST for anything that changes data or contains sensitive information like passwords.

Input Types

HTML offers many <输入> types, each with built-in behavior and validation:

  • 文本 — single-line text.
  • email — validates the format of an email address.
  • 密码 — masks characters as you type.
  • 数字 — numeric input with up/down steppers.
  • checkbox — a toggle (on/off).
  • radio — one choice from a group (all radios share the same name).
  • <textarea> — multi-line text (not an <输入>, its own element).
  • <选择> — a dropdown menu.

Labels and Accessibility

Every input should have a <label> connected via the for attribute matching the input's id. This is not optional — it is essential for accessibility. Screen readers announce the label when the input gets focus, and clicking the label focuses the input. A form without labels is unusable for keyboard and screen reader users.

Built-in Validation

HTML can validate input without JavaScript. Add 必需 to force a field, 最小值 and 最大值 for numeric ranges, maxlength for text limits, and 模式 for a regex the value must match. The browser blocks submission and shows an error bubble if validation fails. This covers most common cases — reach for JavaScript validation only when you need custom rules or cross-field checks.

示例代码

              <form action="/api/register" method="POST">
  <div>
    <label for="name">Full Name</label>
    <input type="text" id="name" name="name" required />
  </div>

  <div>
    <label for="email">Email</label>
    <input type="email" id="email" name="email" required />
  </div>

  <div>
    <label for="age">Age</label>
    <input type="number" id="age" name="age" min="13" max="120" />
  </div>

  <div>
    <label for="bio">Bio (max 200 chars)</label>
    <textarea id="bio" name="bio" maxlength="200"></textarea>
  </div>

  <button type="submit">Register</button>
</form>
            

每个输入有 已连接 label 通过 for/id. email 输入验证格式 automatically. Age 是 constrained 到 13–120. textarea 限制长度. 必需 attribute prevents submission 的空 name and email 字段.

动手试一试

  • HTML ↔ Markdown

    See 如何表单 HTML converts 到 and 从 Markdown 结构.

  • JSON Formatter

    表单 submissions 经常 produce JSON — 格式 result 到 inspect 它.

常见错误

错误写法

Forgetting 到连接 <label> 到 <输入> 使用 for and id.

正确做法

总是设置 <label for="x"> and <输入 id="x">. 不使用这个, screen readers cannot announce 字段, and 点击 label 将不 focus 输入.

错误写法

Using GET for 登录 or registration 表单.

正确做法

GET puts 表单数据 in URL, exposing 密码 in 浏览器 history and 服务器 logs. 总是 use 方法="POST" for sensitive 数据, and serve 表单 over HTTPS.

相关代码片段

相关速查表

练习 HTML 表单

4 个练习

练习 HTML 表单

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误