HTMLInputElement

HTMLInputElement 接口提供了特定的属性和方法(继承自常规的HTML元素接口)用于管理输入元素的布局和外观。

属性

form 只读 HTMLFormElement object: 返回一个父表单元素的引用。
formAction string: 返回/ 设置 元素的 formaction 属性, 包含处理元素提交信息程序的 URI. 这会重写父表单的 action 属性.
formEncType string: 返回/ 设置 元素的 formenctype 属性, 包含将表单提交到服务器的内容类型. 这会重写父表单的 enctype 属性.
formMethod string: 返回/ 设置 元素的 formmethod 属性, 包含浏览器用于提交表单的 HTTP 方法. 这会重写父表单的 method 属性.
formNoValidate boolean: 返回/ 设置 元素的 formnovalidate 属性, 表示在表单提交时不对其进行验证. 这会重写父表单的 novalidate 属性.
formTarget string: 返回/ 设置 元素的 formtarget 属性, 包含一个名称或关键字, 表示在提交表单后接收响应的显示位置. 这会重写父表单的 target 属性.
name string: 返回/ 设置 元素的 name 属性, 包含其名称.
type string: 返回/ 设置 元素的 type 属性, 包含其显示类型. 查看 <input>type 属性可用值.
disabled boolean: 返回/ 设置 元素的 disabled 属性, 表示是否禁用 <input>. 该元素的值将不会被提交. 也可以查看 readonly
autofocus boolean: 返回/ 设置 元素的 autofocus 属性, 指定的 <input> 在页面加载时应当具有输入焦点, 例如通过键入不同的控件. 在文档中只有一个表单元素可以拥有 autofocus 属性. 如果 type 属性被设置为 hidden 则无效(即不可为隐藏控件设置自动焦点).
required boolean: 返回/ 设置 元素的 required 属性, 表示用户必填项.
value string: 返回/ 设置 当前控件的值.提示: 如果用户输入与预期不同,可能会返回空.
validity 只读 ValidityState object: Returns the validity state that this element is in.
validationMessage 只读 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 (willvalidate is false), or it satisfies its constraints.
willValidate 只读 Boolean: Indicates whether the element is a candidate for constraint validation. It is false if any conditions bar it from constraint validation.
checked boolean: 返回/ 设置 当前选中状态,当控件typecheckboxradio时.
defaultChecked boolean: 返回/ 设置 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. Changes the appearance to resemble a third state. Does not affect the value of the checked 属性, and clicking the checkbox will set the value to false.
alt string: 返回/ 设置 元素的 alt 属性, 包含 alternative text to use when type is image.
height string: 返回/ 设置 元素的 height 属性, which defines the height of the image displayed for the button, if the value of type is image.
src string: 返回/ 设置 元素的 src 属性, which specifies a URI for the location of an image to display on the graphical submit button, if the value of type is image; otherwise it is ignored.
width string: 返回/ 设置 the document's width 属性, which defines the width of the image displayed for the button, if the value of type is image.
accept string: Returns / Sets 元素的 accept 属性, 包含 comma-separated list of file types accepted by the server when type is file.
allowdirs 非标准 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.
files Returns/accepts a FileList object, which contains a list of File objects representing the files selected for upload.
webkitdirectory 非标准 boolean: Returns the webkitdirectory 属性; if true, the file system picker interface only accepts directories instead of files.
webkitEntries (en-US) 非标准 Array of FileSystemEntry objects describing the currently-selected files or directories.
autocomplete string: 返回/ 设置 元素的 autocomplete 属性, indicating whether the value of the control can be automatically completed by the browser. Ignored if the value of the type 属性 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: 返回/ 设置 元素的 maxlength 属性, 包含 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: 返回/ 设置 元素的 size 属性, 包含 size of the control. This value is in pixels unless the value of type is text or password, in which case, it is an integer number of characters. Applies only when type is set to text, search, tel, url, email, or password; otherwise it is ignored.
pattern string: 返回/ 设置 元素的 pattern 属性, 包含 a regular expression that the control's value is checked against. Use the title 属性 to describe the pattern to help the user. This 属性 applies when the value of the type 属性 is text, search, tel, url or email; otherwise it is ignored.
placeholder string: 返回/ 设置 元素的 placeholder 属性, 包含 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 属性 applies when the value of the type 属性 is text, search, tel, url or email; otherwise it is ignored.
readOnly boolean: 返回/ 设置 元素的 readonly 属性, indicating that the user cannot modify the value of the control. This is ignored if the value of the type 属性 is hidden, range, color, checkbox, radio, file, or a button type.
min string: 返回/ 设置 元素的 min 属性, 包含 the minimum (numeric or date-time) value for this item, which must not be greater than its maximum (max 属性) value.
max string: 返回/ 设置 元素的 max 属性, 包含 the maximum (numeric or date-time) value for this item, which must not be less than its minimum (min 属性) value.
selectionStart unsigned long: 返回/ 设置 the beginning index of the selected text. When nothing is selected, this returns the position of the text input cursor (caret) inside of the <input> element.
selectionEnd unsigned long: 返回/ 设置 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: 返回/ 设置 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."
未分类的属性
defaultValue string: 返回/ 设置 the default value as originally specified in the HTML that created this object.
dirName string: 返回/ 设置 the directionality of the element.
accessKey string: 返回 a string 包含 a single character that switches input focus to the control when pressed.
list 只读 HTMLElement object: 返回 the element pointed by the list 属性. The property may be null if no HTML element found in the same tree.
multiple boolean: 返回/ 设置 元素的 multiple 属性, indicating whether more than one value is possible (e.g., multiple files).
files FileList array: 返回the list of selected files.
HTMLInputElement.labels 只读 NodeList array: 返回a list of <label> elements that are labels for this element.
step string: 返回/ 设置 元素的 step 属性, which works with min and max 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 Date object: Returns / Sets the value of the element, interpreted as a date, or null if conversion is not possible.
valueAsNumber double: 返回the value of the element, interpreted as one of the following, in order:
  • a time value
  • a number
  • NaN if conversion is impossible
autocapitalize 实验性 string: 定义 the capitalization behavior for user input. Valid values are none, off, characters, words, or sentences.
HTMLInputElement.align 已弃用

string: represents the alignment of the element. Use CSS instead.

HTMLInputElement.useMap 已弃用

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()

Replaces a range of text with the new text. If the start and end arguments are not provided, the range is assumed to be the selection.The final argument determines how the selection should be set after the text has been replaced. The possible values are:

"select"
Selects the newly inserted text.
"start"
Moves the selection to just before the inserted text.
"end"
Moves the selection to just after the selected text.
"preserve"
Attempts to preserve the selection. This is the default.
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 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 invalid (en-US) event at the element. It returns true if the element is not a candidate for constraint validation, or if it satisfies its constraints.
HTMLInputElement.stepDown() (en-US)

Decrements the value by (step * n), where n defaults to 1 if not specified. Throws an INVALID_STATE_ERR exception:

  • if the method is not applicable to for the current type value,
  • if the element has no step value,
  • if the value cannot be converted to a number,
  • if the resulting value is above the max or below the min.
HTMLInputElement.stepUp() (en-US)

Increments the value by (step * n), where n defaults to 1 if not specified. Throws an INVALID_STATE_ERR exception:

  • if the method is not applicable to for the current type value.,
  • if the element has no step value,
  • if the value cannot be converted to a number,
  • if the resulting value is above the max or below the min.
HTMLInputElement.mozSetFileArray() 非标准

Sets the files selected on the input to the given array of 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.

HTMLInputElement.mozGetFileNameArray() (en-US) 非标准

Returns an array of all the file names from the input.

HTMLInputElement.mozSetFileNameArray() (en-US) 非标准

Sets the filenames for the files selected on the input. Not for use in frame scripts, because it accesses the file system.

规范

Specification
HTML Standard
# htmlinputelement

浏览器兼容性

BCD tables only load in the browser

参见

  • HTML element implementing this interface: <input>.