Movatterモバイル変換


[0]ホーム

URL:


  1. 面向开发者的 Web 技术
  2. Web API
  3. HTMLInputElement
  4. HTMLInputElement: invalid event

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in EnglishAlways switch to English

HTMLInputElement: invalid event

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2018年12月⁩.

若一个可提交元素在检查有效性时,不符合对它的约束条件,则会触发invalid 事件。

冒泡
可取消
接口Event
事件处理程序属性GlobalEventHandlers.oninvalid

这个事件可用于展示提交表单时所出现的问题的概览。当表单提交时,若任一表单控件无效,则会触发invalid 事件。对可提交元素有效性的检查是在提交父元素<form> 之前或调用父元素<form> 或元素自己的checkValidity() 方法之后。

这个事件不会在blur 事件中触发。

示例

如果表单提交时有无效值,检查可提交元素时发现了错误,则invalid 事件会在那个无效元素上触发。在这个例子中,当输入无效值触发invalid 事件时,这个无效值被记录下来。

HTML

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></p>

JavaScript

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

结果

规范

Specification
HTML
# event-invalid
HTML
# handler-oninvalid

浏览器兼容性

参见

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp