标准答案

  1. type 决定控件的输入类型和浏览器能力,比如移动端键盘、默认校验、输入限制和可访问性语义。
  2. name 决定表单提交时的字段名。没有 name 的控件,即使有 value,也不会进入原生表单提交结果。
  3. radio 同一组必须使用相同 name,浏览器才知道它们互斥;checkbox 同名字段可能提交多个值。
  4. autocomplete 告诉浏览器字段对应的用户信息,比如 email、current-password、new-password、one-time-code。
  5. 所以 type 不只是 UI,name 不只是给后端看,autocomplete 也不应该随便写 on/off;它们共同影响提交、体验、可访问性和安全。

题目解析

  • type:决定输入类型、移动端键盘、浏览器内置校验和默认行为。
  • name:决定提交时的字段名,也是 radio 分组和 checkbox 多值提交的关键。
  • autocomplete:提示浏览器自动填充语义,影响密码管理器、验证码和地址表单体验。

这题不要只背 type 的枚举。真正的考点是浏览器如何根据表单语义参与输入、校验、提交和自动填充。

如果 name 缺失,前端看起来能输入,后端却收不到字段;如果 autocomplete 写错,密码管理器可能把注册密码当成登录密码,也可能不能识别一次性验证码字段。

代码示例

登录表单里,email、password 和 autocomplete 应该表达真实字段含义。

HTML
<form action="/login" method="post">
  <label for="email">邮箱</label>
  <input
    id="email"
    name="email"
    type="email"
    autocomplete="email"
    required
  >

  <label for="password">密码</label>
  <input
    id="password"
    name="password"
    type="password"
    autocomplete="current-password"
    required
  >

  <button type="submit">登录</button>
</form>

常见误区

  • 只给 input 写 id,不写 name,导致表单提交没有对应字段。
  • 把所有输入框都写成 type="text",丢失 email、number、password 等浏览器能力。
  • 登录密码和注册新密码都写 autocomplete="password",导致密码管理器识别不准。

作者信息