<form>
HTML <form>
元素表示提交訊息的區塊,此區塊含有許多可互動的控制項。
嘗試一下
It is possible to use the :valid
(en-US) and :invalid
(en-US) CSS pseudo-classes to style a <form>
element based on whether or not the elements
(en-US) inside the form are valid.
Content categories | Flow content, palpable content |
---|---|
Permitted content | Flow content, but not containing <form> elements |
Tag omission | None, both the starting and ending tag are mandatory. |
Permitted parents | Any element that accepts flow content |
Implicit ARIA role | form (en-US) if the form has an accessible name, otherwise no corresponding role |
Permitted ARIA roles | search (en-US), none (en-US) or presentation (en-US) |
DOM interface | HTMLFormElement |
Attributes
This element includes the global attributes.
accept
已棄用-
Comma-separated content types (en-US) 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 (en-US). (In previous versions of HTML, character encodings could also be delimited by commas.)
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">
(en-US) element. autocapitalize
非標準-
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 (en-US).)on
: The browser may automatically complete entries.
enctype
-
If the value of the
method
attribute ispost
,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 withtype=file
.text/plain
: Introduced by HTML5 for debugging purposes.This value can be overridden byformenctype
attributes on<button>
,<input type="submit">
, or<input type="image">
(en-US) elements.
method
-
The HTTP method to submit the form with. Possible values:
post
: The POST method; form data sent as the request body.get
: The GET method; form data appended to theaction
URL with a?
separator. Use this method when the form has no side-effects (en-US).dialog
: When the form is inside a<dialog>
(en-US), closes the dialog on submission.This value is overridden byformmethod
attributes on<button>
,<input type="submit">
, or<input type="image">
(en-US) elements.
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. 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">
(en-US) element belonging to the form. rel
-
Creates a hyperlink or annotation depending on the value, see the
rel
attribute for details. 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 aformtarget
attribute on a<button>
,<input type="submit">
, or<input type="image">
(en-US) 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
Specification |
---|
HTML Standard # the-form-element |
Browser compatibility
BCD tables only load in the browser
See also
- HTML forms guide
- Other elements that are used when creating forms:
<button>
,<datalist>
(en-US),<fieldset>
(en-US),<input>
,<label>
(en-US),<legend>
(en-US),<meter>
,<optgroup>
,<option>
(en-US),<output>
(en-US),<progress>
(en-US),<select>
(en-US),<textarea>
(en-US). - Getting a list of the elements in the form:
HTMLFormElement.elements
(en-US) - ARIA: Form role (en-US)
- ARIA: Search role (en-US)