input event fires when the value of an <input> , <select> ,或 <textarea> 元素已改变。

冒泡 Yes
可取消 No
接口 InputEvent
事件处理程序特性 GlobalEventHandlers.oninput

The event also applies to elements with contenteditable enabled, and to any element when designMode is turned on. In the case of contenteditable and designMode , the event target is the editing host . If these properties apply to multiple elements, the editing host is the nearest ancestor element whose parent isn't editable.

For <input> elements with type=checkbox or type=radio input event should fire whenever a user toggles the control, per the HTML5 specification . However, historically this has not always been the case. Check compatibility, or use the change event instead for elements of these types.

注意: input event is fired every time the value of the element changes. This is unlike the change event, which only fires when the value is committed, such as by pressing the enter key, selecting a value from a list of options, and the like.

范例

This example logs the value whenever you change the value of the <input> 元素。

HTML

<input placeholder="Enter some text" name="name"/>
<p id="values"></p>
					

JavaScript

const input = document.querySelector('input');
const log = document.getElementById('values');
input.addEventListener('input', updateValue);
function updateValue(e) {
  log.textContent = e.target.value;
}
					

结果

规范

规范 状态
HTML 实时标准
The definition of 'input event' in that specification.
实时标准
DOM (文档对象模型) 3 级事件规范
The definition of 'input event' in that specification.
过时

浏览器兼容性

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. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
input event Chrome 1 Edge 79
79
不支持 12 — 79
Not supported on select , checkbox ,或 radio inputs.
Firefox 6 IE 部分支持 9
部分支持 9
Only supports input 类型 text and password .
Opera 11.6 Safari 3.1 WebView Android 1 Chrome Android 18 Firefox Android 6 Opera Android 12 Safari iOS 2 Samsung Internet Android 1.0

图例

完整支持

完整支持

部分支持

部分支持

见实现注意事项。

另请参阅

元数据

  • 最后修改: