<form> - HTML: HyperText Markup Language 编辑

The HTML <form> element represents a document section containing interactive controls for submitting information.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

It is possible to use the :valid and :invalid CSS pseudo-classes to style a <form> element based on whether or not the elements inside the form are valid.

Content categoriesFlow content, palpable content
Permitted contentFlow content, but not containing <form> elements
Tag omissionNone, both the starting and ending tag are mandatory.
Permitted parentsAny element that accepts flow content
Implicit ARIA roleform if the form has an accessible name, otherwise no corresponding role
Permitted ARIA rolessearch, none or presentation
DOM interfaceHTMLFormElement

Attributes

This element includes the global attributes.

accept This is an obsolete API and is no longer guaranteed to work.
Comma-separated content types the server accepts. This attribute was removed in HTML5 and should not be used. Instead, use the accept attribute on <input type=file> elements.
accept-charset
Space-separated character encodings the server accepts. The browser uses them in the order in which they are listed. The default value means the same encoding as the page.
(In previous versions of HTML, character encodings could also be delimited by commas.)
autocapitalize This API has not been standardized.
A nonstandard attribute used by iOS Safari that controls how textual form elements should be automatically capitalized. autocapitalize attributes on a form elements override it on <form>. Possible values:
  • none: No automatic capitalization.
  • sentences (default): Capitalize the first letter of each sentence.
  • words: Capitalize the first letter of each word.
  • characters: Capitalize all characters — that is, uppercase.
autocomplete
Indicates whether input elements can by default have their values automatically completed by the browser. autocomplete attributes on form elements override it on <form>. Possible values:
  • off: The browser may not automatically complete entries. (Browsers tend to ignore this for suspected login forms; see The autocomplete attribute and login fields.)
  • on: The browser may automatically complete entries. 
name
The name of the form. Deprecated as of HTML 4 (use id instead). It must be unique among the forms in a document and not an empty string as of HTML5.
rel
Creates a hyperlink or annotation depending on the value, see the rel attribute for details.

Attributes for form submission

The following attributes control behavior during form submission.

action
The URL that processes the form submission. This value can be overridden by a formaction attribute on a <button>, <input type="submit">, or <input type="image"> element.
enctype
If the value of the method attribute is post, enctype is the MIME type of the form submission. Possible values:
  • application/x-www-form-urlencoded: The default value.
  • multipart/form-data: Use this if the form contains <input> elements with type=file.
  • text/plain: Introduced by HTML5 for debugging purposes.

This value can be overridden by formenctype attributes on <button>, <input type="submit">, or <input type="image"> elements.

method
The HTTP method to submit the form with. Possible (case insensitive) values:

This value is overridden by formmethod attributes on <button><input type="submit">, or <input type="image">elements.

novalidate
This Boolean attribute indicates that the form shouldn't be validated when submitted. If this attribute is not set (and therefore the form is validated), it can be overridden by a formnovalidate attribute on a <button><input type="submit">, or <input type="image"> element belonging to the form.
target
Indicates where to display the response after submitting the form. In HTML 4, this is the name/keyword for a frame. In HTML5, it is a name/keyword for a browsing context (for example, tab, window, or iframe). The following keywords have special meanings:
  • _self (default): Load into the same browsing context as the current one.
  • _blank: Load into a new unnamed browsing context.
  • _parent: Load into the parent browsing context of the current one. If no parent, behaves the same as _self.
  • _top: Load into the top-level browsing context (i.e., the browsing context that is an ancestor of the current one and has no parent). If no parent, behaves the same as _self.

This value can be overridden by a formtarget attribute on a <button><input type="submit">, or <input type="image"> element.

Examples

HTML

<!-- 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>

Specifications

SpecificationStatusComment
HTML Living Standard
The definition of '<form>' in that specification.
Living Standard
HTML5
The definition of '<form>' in that specification.
Recommendation
HTML 4.01 Specification
The definition of '<form>' in that specification.
RecommendationInitial definition

Browser compatibility

BCD tables only load in the browser

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:85 次

字数:17453

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文