正在加载...

HTML input 的 form 属性

控件通常在 <form> 内。布局有时把字段放在外面。form 可通过 id 仍关联到表单。

直接回答

给 form 设 id,在 input 上写 form="that-id"。提交时该控件数据随该 form。submit 上的 formaction 可只改该按钮的 action。

Input outside its form

html
HTML Code
<form id="signup" action="/signup" method="post">
<label for="user">Username</label>
<input id="user" name="user" type="text">
<button type="submit">Create</button>
</form>

<p>Extra field placed later in the layout:</p>
<label for="promo">Promo</label>
<input id="promo" name="promo" type="text" form="signup">

试一试

  1. 带 id 的 form
  2. 闭合标签后放 input
  3. 设置 form
  4. 确认归属模型

常见错误

  • id 拼写错误
  • 以为 form 代替 name
  • 过多 detached 字段

小结

  • form="id" 关联控件
  • 特殊布局有用
  • 仍需 name 和 label
  • 能嵌套时更清晰

🧠 测试您的知识

准备开始

测试您的知识

通过这个互动测验挑战自己,看看你对这个主题的理解程度如何

❓
4
问题
🎯
70%
及格要求
♾️
∞
时间
🔄
∞
尝试次数

📝 说明

  • 仔细阅读每个问题
  • 为每个问题选择最佳答案
  • 您可以随时重新参加测验
  • 您的进度将显示在顶部