无效 event fires when a submittable element has been checked for validity and doesn't satisfy its constraints.

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

This event can be useful for displaying a summary of the problems with a form on submission. When a form is submitted, 无效 events are fired at each form control that is invalid. The validity of submittable elements is checked before submitting their owner <form> , or after the checkValidity() method of the element or its owner <form> 被调用。

It is not checked on blur .

范例

If a form is submitted with an invalid value, the submittable elements are checked and, if an error is found, the 无效 event will fire on the 无效 element. In this example, when an invalid event fires because of an invalid value in the input, the invalid value is logged.

HTML

<form action="#">
  <ul>
    <li><label>Enter an integer between 1 and 10: <input type="number" min="1" max="10" required></label></li>
    <li><input type="submit" value="submit"></li>
  </ul>
</form><p id="log"></p>
					

JavaScript

const input = document.querySelector('input')
const log = document.getElementById('log')
input.addEventListener('invalid', logValue)
function logValue(e) {
  log.textContent += e.target.value
}
					

结果

规范

规范 状态 注释
HTML 实时标准
The definition of 'Invalid event' in that specification.
实时标准
HTML 5.1
The definition of 'Invalid event' in that specification.
推荐
HTML5
The definition of 'Invalid event' in that specification.
推荐

浏览器兼容性

The compatibility table on 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
无效 event Chrome 10 Edge 12 Firefox 4 IE 10 Opera 10 Safari 5 WebView Android 4 Chrome Android 18 Firefox Android 64 Opera Android 12 Safari iOS 5 Samsung Internet Android 4.0

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改: