--- title: form slug: Web/HTML/Element/form tags: - Forms - HTML - HTML 表单 - Web - 元素 - 参考 - 表单 translation_of: Web/HTML/Element/form ---
HTML <form> 元素表示文档中的一个区域,此区域包含交互控件,用于向 Web 服务器提交信息。
可以用 {{cssxref(':valid')}} 和 {{cssxref(':invalid')}} CSS 伪类来设置 <form> 元素的样式,此时样式的表现取决于表单中的 {{domxref("HTMLFormElement.elements", "elements")}} 是否有效。
| 内容类别 | Flow content, palpable content |
|---|---|
| 可包含内容 | Flow content, but not containing <form> elements |
| 标签省略 | {{no_tag_omission}} |
| 允许的父级元素 | 可以是 HTML 的任何标签 |
| Implicit ARIA role | form if the form has an accessible name, otherwise no corresponding role |
| Permitted ARIA roles | search, {{ARIARole("group")}}, {{ARIARole("presentation")}} |
| DOM 接口 | {{domxref("HTMLFormElement")}} |
此元素拥有 全局属性。
<input type=file> 元素中的 {{htmlattrxref("accept", "input")}} 属性。"UNKNOWN"。此字符串指的是,和包含此表单元素的文档相同的编码。autocapitalize 属性在某个单独的表单后代控件被指定的话,那么此单独的设定会覆盖原来表单范围内的 autocapitalize 设定。默认值为 sentences。可以选择的值如下:
none:完全禁用自动首字母大写。sentences:自动对每句话首字母大写。words:自动对每个单词首字母大写。characters:自动大写所有的字母。autocomplete 属性覆盖。 可能的值有:
off:浏览器可能不会自动补全条目(在疑似登录表单中,浏览器倾向于忽略该值,而提供密码自动填充功能,参见 自动填充属性和登录)on:浏览器可自动补全条目id)。在 HTML 5 中,该值必须是所有表单中独一无二的,而且不能是空字符串。下列属性控制提交表单时的行为。
<input type="submit"> 或 <input type="image"> 元素上的 {{htmlattrxref("formaction", "button")}} 属性覆盖。method 属性值为 post 时,enctype 就是将表单的内容提交给服务器的 MIME 类型 。可能的取值有:
application/x-www-form-urlencoded:未指定属性时的默认值。multipart/form-data:当表单包含 type=file 的 {{HTMLElement("input")}} 元素时使用此值。text/plain:出现于 HTML5,用于调试。这个值可被 {{HTMLElement("button")}}、<input type="submit"> 或 <input type="image"> 元素上的 {{htmlattrxref("formenctype", "button")}} 属性覆盖。
post:指的是 HTTP POST 方法;表单数据会包含在表单体内然后发送给服务器。get:指的是 HTTP GET 方法;表单数据会附加在 action 属性的 URL 中,并以 '?' 作为分隔符,没有副作用 时使用这个方法。dialog:如果表单在 {{HTMLElement("dialog")}} 元素中,提交时关闭对话框。此值可以被 {{HTMLElement("button")}}、<input type="submit"> 或 <input type="image"> 元素中的 {{htmlattrxref("formmethod", "button")}} 属性覆盖。
<input type="submit"> 或 <input type="image"> 元素中的 {{htmlattrxref("formnovalidate", "button")}} 属性覆盖。_self:默认值。在相同浏览上下文中加载。_blank:在新的未命名的浏览上下文中加载。_parent:在当前上下文的父级浏览上下文中加载,如果没有父级,则与 _self 表现一致。_top:在最顶级的浏览上下文中(即当前上下文的一个没有父级的祖先浏览上下文),如果没有父级,则与 _self 表现一致。此值可以被 {{HTMLElement("button")}}、 <input type="submit"> 或 <input type="image"> 元素中的 {{htmlattrxref("formtarget", "button")}} 属性覆盖。
<!-- Form which will send a GET request to the current URL -->
<form>
<label>Name:
<input name="submitted-name" autocomplete="name">
</label>
<button>Save</button>
</form>
<!-- Form which will send a POST request to the current URL -->
<form method="post">
<label>Name:
<input name="submitted-name" autocomplete="name">
</label>
<button>Save</button>
</form>
<!-- Form with fieldset, legend, and label -->
<form method="post">
<fieldset>
<legend>Title</legend>
<label><input type="radio" name="radio"> Select me</label>
</fieldset>
</form>
{{ EmbedLiveSample('Examples', '100%', 110) }}
| 规范 | 状态 | 备注 |
|---|---|---|
| {{SpecName('HTML WHATWG', 'forms.html#the-form-element', '<form>')}} | {{Spec2('HTML WHATWG')}} | |
| {{SpecName('HTML5 W3C', 'forms.html#the-form-element', '<form>')}} | {{Spec2('HTML5 W3C')}} | |
| {{SpecName('HTML4.01', 'interact/forms.html#h-17.3', '<form>')}} | {{Spec2('HTML4.01')}} | Initial definition |
{{Compat("html.elements.form")}}
对于自动完成的请求,Google Chrome UI 是不一样的,取决于在 input 元素及其表单上,autocomplete 是否设为 off 。特别是,如果表单的 autocomplete 设为 off ,并且它的输入元素的 autocomplete 字段没有设置,如果用户请求该输入元素的自动填充建议,Chrome 就可能展示一个消息,“自动完成已关闭”。另一方面,如果表单和输入元素的 autocomplete 都设为 off,浏览器就不会展示此消息。出于此原因,你应该将每个拥有自定义自动完成的输入元素的 autocomplete 设为 off 。