--- title: HTMLInputElement slug: Web/API/HTMLInputElement translation_of: Web/API/HTMLInputElement ---
{{ APIRef("HTML DOM") }}

HTMLInputElement 介面提供了特殊的屬性及方法以操作 input 元素的顯示與佈局。

{{InheritanceDiagram(600,120)}}

屬性

Properties related to the parent form
form {{readonlyInline}} {{domxref("HTMLFormElement")}} object:  Returns a reference to the parent form element.
formAction string: Returns / Sets the element's {{ htmlattrxref("formaction", "input") }} attribute, containing the URI of a program that processes information submitted by the element. This overrides the {{ htmlattrxref("action", "form") }} attribute of the parent form.
formEncType string: Returns / Sets the element's {{ htmlattrxref("formenctype", "input") }} attribute, containing the type of content that is used to submit the form to the server. This overrides the {{ htmlattrxref("enctype", "form") }} attribute of the parent form.
formMethod string: Returns / Sets the element's {{ htmlattrxref("formmethod", "input") }} attribute, containing the HTTP method that the browser uses to submit the form. This overrides the {{ htmlattrxref("method", "form") }} attribute of the parent form.
formNoValidate boolean: Returns / Sets the element's {{ htmlattrxref("formnovalidate", "input") }} attribute, indicating that the form is not to be validated when it is submitted. This overrides the {{ htmlattrxref("novalidate", "form") }} attribute of the parent form.
formTarget string: Returns / Sets the element's {{ htmlattrxref("formtarget", "input") }} attribute, containing a name or keyword indicating where to display the response that is received after submitting the form. This overrides the {{ htmlattrxref("target", "form") }} attribute of the parent form.
Properties that apply to any type of input element that is not hidden
name string: Returns / Sets the element's {{ htmlattrxref("name", "input") }} attribute, containing a name that identifies the element when submitting the form.
type string: Returns / Sets the element's {{ htmlattrxref("type", "input") }} attribute, indicating the type of control to display. See {{ htmlattrxref("type", "input") }} attribute of {{ HTMLElement("input") }} for possible values.
disabled boolean: Returns / Sets the element's {{ htmlattrxref("disabled", "input") }} attribute, indicating that the control is not available for interaction. The input values will not be submitted with the form. See also {{ htmlattrxref("readOnly", "input") }} 
autofocus boolean: Returns / Sets the element's {{ htmlattrxref("autofocus", "input") }} attribute, which specifies that a form control should have input focus when the page loads, unless the user overrides it, for example by typing in a different control. Only one form element in a document can have the {{htmlattrxref("autofocus","input")}} attribute. It cannot be applied if the {{htmlattrxref("type","input")}} attribute is set to hidden (that is, you cannot automatically set focus to a hidden control).
required boolean: Returns / Sets the element's {{ htmlattrxref("required", "input") }} attribute, indicating that the user must fill in a value before submitting a form.
value string: Returns / Sets the current value of the control.

Note: If the user enters a value different from the value expected, this may return an empty string.

validity {{readonlyInline}} {{domxref("ValidityState")}} object: Returns the validity state that this element is in.
validationMessage {{readonlyInline}} string: Returns a localized message that describes the validation constraints that the control does not satisfy (if any). This is the empty string if the control is not a candidate for constraint validation ({{htmlattrxref("willValidate","input")}} is false), or it satisfies its constraints.
willValidate {{readonlyInline}} {{jsxref("Boolean")}}: Indicates whether the element is a candidate for constraint validation. It is false if any conditions bar it from constraint validation.
Properties that apply only to elements of type "checkbox" or "radio"
checked  boolean: Returns / Sets the current state of the element when {{htmlattrxref("type","input")}} is checkbox or radio.
defaultChecked boolean: Returns / Sets the default state of a radio button or checkbox as originally specified in HTML that created this object.
indeterminate boolean: indicates that the checkbox is neither on nor off.
Properties that apply only to elements of type "image"
alt string: Returns / Sets the element's {{ htmlattrxref("alt", "input") }} attribute, containing alternative text to use when {{htmlattrxref("type","input")}} is image.
height string: Returns / Sets the element's {{ htmlattrxref("height", "input") }} attribute, which defines the height of the image displayed for the button, if the value of {{htmlattrxref("type","input")}} is image.
src string: Returns / Sets the element's {{ htmlattrxref("src", "input") }} attribute, which specifies a URI for the location of an image to display on the graphical submit button, if the value of {{htmlattrxref("type","input")}} is image; otherwise it is ignored.
width string: Returns / Sets the document's {{ htmlattrxref("width", "input") }} attribute, which defines the width of the image displayed for the button, if the value of {{htmlattrxref("type","input")}} is image.
Properties that apply only to elements of type "file"
accept string: Returns / Sets the element's {{ htmlattrxref("accept", "input") }} attribute, containing comma-separated list of file types accepted by the server when {{htmlattrxref("type","input")}} is file.
allowdirs {{non-standard_inline}} boolean: Part of the non-standard Directory Upload API; indicates whether or not to allow directories and files both to be selected in the file list. Implemented only in Firefox and is hidden behind a preference.
{{domxref("HTMLInputElement.webkitdirectory", "webkitdirectory")}} {{Non-standard_inline}} boolean: Returns the {{htmlattrxref("webkitdirectory", "input")}} attribute; if true, the file system picker interface only accepts directories instead of files.
{{domxref("HTMLInputElement.webkitEntries", "webkitEntries")}} {{Non-standard_inline}} Array of {{domxref("FileSystemEntry")}} objects describing the currently-selected files or directories.
Properties that apply only to text/number-containing or elements
autocomplete string: Returns / Sets the element's {{htmlattrxref("autocomplete", "input")}} attribute, indicating whether the value of the control can be automatically completed by the browser. Ignored if the value of the {{htmlattrxref("type","input")}} attribute is hidden, checkbox, radio, file, or a button type (button, submit, reset, image). Possible values are:
"on": the browser can autocomplete the value using previously stored value
"off": the user must explicity enter a value
maxLength long: Returns / Sets the element's {{ htmlattrxref("maxlength", "input") }} attribute, containing the maximum length of characters (in Unicode code points) that the value can have. (If you set this to a negative number, an exception will be thrown.)
size unsigned long: Returns / Sets the element's {{ htmlattrxref("size", "input") }} attribute, containing size of the control. This value is in pixels unless the value of {{htmlattrxref("type","input")}} is text or password, in which case, it is an integer number of characters. Applies only when {{htmlattrxref("type","input")}} is set to text, search, tel, url, email, or password; otherwise it is ignored.
pattern string: Returns / Sets the element's {{ htmlattrxref("pattern", "input") }} attribute, containing a regular expression that the control's value is checked against.  Use the {{htmlattrxref("title","input")}} attribute to describe the pattern to help the user. This attribute applies when the value of the {{htmlattrxref("type","input")}} attribute is text, search, tel, url or email; otherwise it is ignored.
placeholder string: Returns / Sets the element's {{ htmlattrxref("placeholder", "input") }} attribute, containing a hint to the user of what can be entered in the control. The placeholder text must not contain carriage returns or line-feeds. This attribute applies when the value of the {{htmlattrxref("type","input")}} attribute is text, search, tel, url or email; otherwise it is ignored.
readOnly boolean: Returns / Sets the element's {{ htmlattrxref("readonly", "input") }} attribute, indicating that the user cannot modify the value of the control.
{{HTMLVersionInline(5)}}This is ignored if the value of the {{htmlattrxref("type","input")}} attribute is hidden, range, color, checkbox, radio, file, or a button type.
min string: Returns / Sets the element's {{ htmlattrxref("min", "input") }} attribute, containing the minimum (numeric or date-time) value for this item, which must not be greater than its maximum ({{htmlattrxref("max","input")}} attribute) value.
max string: Returns / Sets the element's {{ htmlattrxref("max", "input") }} attribute, containing the maximum (numeric or date-time) value for this item, which must not be less than its minimum (min attribute) value.
selectionStart unsigned long: Returns / Sets the beginning index of the selected text. When nothing is selected, this returns the position of the text input cursor (caret) inside of the {{HTMLElement("input")}} element.
selectionEnd unsigned long: Returns / Sets the end index of the selected text. When there's no selection, this returns the offset of the character immediately following the current text input cursor position.
selectionDirection string: Returns / Sets the direction in which selection occurred. Possible values are:
"forward" if selection was performed in the start-to-end direction of the current locale,
"backward" for the opposite direction,
"none" if the direction is unknown."
Properties not yet categorized
defaultValue string: Returns / Sets the default value as originally specified in the HTML that created this object.
dirName string: Returns / Sets the directionality of the element.
accessKey string: Returns a string containing a single character that switches input focus to the control when pressed.
list {{readonlyInline}} {{domxref("HTMLElement")}} object: Returns the element pointed by the {{ htmlattrxref("list", "input") }} attribute. The property may be null if no HTML element found in the same tree.
multiple boolean: Returns / Sets the element's {{ htmlattrxref("multiple", "input") }} attribute, indicating whether more than one value is possible (e.g., multiple files).
files {{readonlyInline}} {{domxref("FileList")}} array: Returns the list of selected files.
labels {{readonlyInline}} {{domxref("NodeList")}} array: Returns a list of {{ HTMLElement("label") }} elements that are labels for this element.
step string: Returns / Sets the element's {{ htmlattrxref("step", "input") }} attribute, which works with {{htmlattrxref("min","input")}} and {{htmlattrxref("max","input")}} to limit the increments at which a numeric or date-time value can be set. It can be the string any or a positive floating point number. If this is not set to any, the control accepts only values at multiples of the step value greater than the minimum.
valueAsDate {{jsxref("Date")}} object: Returns / Sets the value of the element, interpreted as a date, or null if conversion is not possible.
valueAsNumber double: Returns the value of the element, interpreted as one of the following, in order:
  • a time value
  • a number
  • NaN if conversion is impossible
autocapitalize {{experimental_inline}} string: defines the capitalization behavior for user input. Valid values are none, off, characters, words, or sentences.
{{domxref("HTMLInputElement.align")}} {{obsolete_inline}}
string: represents the alignment of the element. Use CSS instead.
{{domxref("HTMLInputElement.useMap")}} {{ obsolete_inline }}
string: represents a client-side image map.

方法

focus() Focus on the input element; keystrokes will subsequently go to this element.
blur() Removes focus from input; keystrokes will subsequently go nowhere.
select() Selects the input text in the element, and focuses it so the user can subsequently replace the whole entry.
click() Simulates a click on the element.
setSelectionRange() Selects a range of text in the element (but does not focus it). The optional selectionDirection parameter may be "forward" or "backward" to establish the direction in which selection was set, or "none" if the direction is unknown or not relevant. The default is "none". Specifying a selectionDirection parameter sets the value of the selectionDirection property.
setRangeText() Selects a range of text in the element (but does not focus it). The optional selectionDirection parameter may be "forward" or "backward" to establish the direction in which selection was set, or "none" if the direction is unknown or not relevant. The default is "none". Specifying a selectionDirection parameter sets the value of the selectionDirection property.
setCustomValidity() Sets a custom validity message for the element. If this message is not the empty string, then the element is suffering from a custom validity error, and does not validate.
checkValidity() Returns a {{jsxref("Boolean")}} that is false if the element is a candidate for constraint validation, and it does not satisfy its constraints. In this case, it also fires an {{event("invalid")}} event at the element. It returns true if the element is not a candidate for constraint validation, or if it satisfies its constraints.
{{domxref("HTMLInputElement.stepDown()")}}
Decrements the {{htmlattrxref("value","input")}} by ({{htmlattrxref("step","input")}} * n), where n defaults to 1 if not specified. Throws an INVALID_STATE_ERR exception:
{{domxref("HTMLInputElement.stepUp()")}}
Increments the {{htmlattrxref("value","input")}} by ({{htmlattrxref("step","input")}} * n), where n defaults to 1 if not specified. Throws an INVALID_STATE_ERR exception:
{{domxref("HTMLInputElement.mozSetFileArray()")}}{{non-standard_inline}}
Sets the files selected on the input to the given array of {{domxref("File")}} objects.  This is an alternative to mozSetFileNameArray() which can be used in frame scripts: a chrome script can open files as File objects and send them via message manager.
{{domxref("HTMLInputElement.mozGetFileNameArray()")}}
Returns an array of all the file names from the input.
{{domxref("HTMLInputElement.mozSetFileNameArray()")}}
Sets the filenames for the files selected on the input.  Not for use in frame scripts, because it accesses the file system.

規範

Specification Status Comment
{{SpecName('HTML WHATWG', "forms.html#the-input-element", "HTMLInputElement")}} {{Spec2('HTML WHATWG')}} No change from {{SpecName("HTML5 W3C")}}
{{SpecName('HTML5 W3C', "forms.html#the-input-element", "HTMLInputElement")}} {{Spec2('HTML5 W3C')}} Technically, the  tabindex and accesskey properties, as well as the blur(), click(), and focus() methods, are now defined on {{domxref("HTMLElement")}}.
The following properties are now obsolete: align and useMap.
The following properties have been added: autocomplete, autofocus, dirName, files, formAction, formEncType, formMethod, formNoValidate, formTarget, height, indeterminate, labels, list, max, min, multiple, pattern, placeholder, required, selectionDirection, selectionEnd, selectionStart, step, validationMessage, validity, valueAsDate, valueAsNumber, width, and willValidate.
The following methods have been added: checkValidity(), setCustomValidity(), setSelectionRange(), stepUp(), and stepDown().
{{SpecName('DOM2 HTML', 'html.html#ID-6043025', 'HTMLInputElement')}} {{Spec2('DOM2 HTML')}} The size property is now an unsigned long. The type property must be entirely given in lowercase characters.
{{SpecName('DOM1', 'level-one-html.html#ID-6043025', 'HTMLInputElement')}} {{Spec2('DOM1')}} Initial definition.

瀏覽器相容性

{{CompatibilityTable}}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support {{CompatVersionUnknown}} {{CompatGeckoDesktop(1.0)}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
autocomplete & autofocus properties {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
files property {{CompatVersionUnknown}} {{CompatGeckoDesktop(1.9)}} 10 {{CompatUnknown}} {{CompatUnknown}}
multiple property {{CompatVersionUnknown}} {{CompatGeckoDesktop("1.9.2")}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
indeterminate property {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
list property {{CompatUnknown}} {{CompatVersionUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
formAction, formEncType, formMethod, formTarget properties {{CompatVersionUnknown}} {{CompatGeckoDesktop(2)}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
formNoValidate, validationMessage, validity, willValidate properties and setCustomValidity() and checkValidity() methods. {{CompatVersionUnknown}} {{CompatGeckoDesktop(2)}} 10 {{CompatVersionUnknown}} {{CompatVersionUnknown}}
pattern, placeholder, required properties {{CompatVersionUnknown}} {{CompatGeckoDesktop(2)}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
height and weight properties {{CompatVersionUnknown}} {{CompatGeckoDesktop(16)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
labels property {{CompatChrome(14.0)}} {{CompatNo}} ({{bug("556743")}}) {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
min, max, and step for <input type="number"> {{CompatVersionUnknown}} Experimental, and without specific UI, since {{CompatGeckoDesktop(16)}}: behind the pref dom.experimental_forms {{CompatUnknown}} {{CompatUnknown}} {{CompatVersionUnknown}}
stepUp and stepDown methods {{CompatUnknown}} Experimental since {{CompatGeckoDesktop(16)}}: behind the pref dom.experimental_forms

There are still differences with the latest spec: {{bug(835773)}}
{{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
min, max, and step properties for <input type="range"> {{CompatVersionUnknown}} {{CompatGeckoDesktop(23)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatVersionUnknown}}
min, max, and step properties for <input type="date"> {{CompatVersionUnknown}} Experimental, and without specific UI, since {{CompatGeckoDesktop(20)}}: behind the pref dom.experimental_forms {{CompatUnknown}} {{CompatUnknown}} {{CompatVersionUnknown}}
min, max, and step properties for other date-related type values {{CompatVersionUnknown}} {{CompatNo}} {{CompatUnknown}} {{CompatUnknown}} {{CompatVersionUnknown}}
mozGetFileNameArray() and mozSetFileNameArray() methods {{non-standard_inline}} {{CompatNo}} {{CompatGeckoDesktop("1.9.2")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
mozSetFileArray() method {{non-standard_inline}} {{CompatNo}} {{CompatGeckoDesktop("38")}} {{CompatNo}} {{CompatNo}} {{CompatNo}}
autocapitalize {{CompatChrome(43.0)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}}
Feature Android Android Webview Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile Chrome for Android
Basic support {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatGeckoMobile(1.0)}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}} {{CompatVersionUnknown}}
autocapitalize {{CompatNo}} {{CompatChrome(43.0)}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatUnknown}} {{CompatChrome(43.0)}}

 

參見