aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/htmlinputelement/invalid_event/index.html
blob: 6f1f97a94d79cad2c8bd0e81ed28305ef2a9a4a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
---
title: 'HTMLInputElement: invalid event'
slug: Web/API/HTMLInputElement/invalid_event
tags:
  - Constraint validation
  - Event
  - 事件
  - 参考
  - 提交
  - 表单
translation_of: Web/API/HTMLInputElement/invalid_event
---
<div>{{APIRef}}</div>

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

<table class="properties">
 <tbody>
  <tr>
   <th>冒泡</th>
   <td></td>
  </tr>
  <tr>
   <th>可取消</th>
   <td></td>
  </tr>
  <tr>
   <th>接口</th>
   <td>{{DOMxRef("Event")}}</td>
  </tr>
  <tr>
   <th>事件处理程序属性</th>
   <td>{{domxref("GlobalEventHandlers.oninvalid")}}</td>
  </tr>
 </tbody>
</table>

<p>这个事件可用于展示提交表单时所出现的问题的概览。当表单提交时,若任一表单控件无效,则会触发 <code>invalid</code> 事件。对可提交元素有效性的检查是在提交父元素 {{HtmlElement("form")}} 之前或调用父元素 <code>&lt;form&gt;</code> 或元素自己的 <a href="/en-US/docs/HTML/Forms_in_HTML#Constraint_Validation_API" title="/en-US/docs/HTML/Forms_in_HTML#Constraint_Validation_API"><code>checkValidity()</code></a> 方法之后。</p>

<p>这个事件不会在 {{Event("blur")}} 事件中触发。</p>

<h2 id="示例">示例</h2>

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

<h3 id="HTML">HTML</h3>

<pre class="brush: html">&lt;form action="#"&gt;
  &lt;ul&gt;
    &lt;li&gt;&lt;label&gt;Enter an integer between 1 and 10: &lt;input type="number" min="1" max="10" required&gt;&lt;/label&gt;&lt;/li&gt;
    &lt;li&gt;&lt;input type="submit" value="submit"&gt;&lt;/li&gt;
  &lt;/ul&gt;
&lt;/form&gt;&lt;p id="log"&gt;&lt;/p&gt;</pre>

<h3 id="JavaScript">JavaScript</h3>

<pre class="brush: js">const input = document.querySelector('input')
const log = document.getElementById('log')

input.addEventListener('invalid', logValue)

function logValue(e) {
  log.textContent += e.target.value
}</pre>

<h3 id="结果">结果</h3>

<p><iframe class="live-sample-frame sample-code-frame" frameborder="0" id="frame_Examples" src="https://mdn.mozillademos.org/en-US/docs/Web/API/HTMLInputElement/invalid_event$samples/Examples?revision=1602988"></iframe></p>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">注释</th>
  </tr>
  <tr>
   <td>{{ SpecName('HTML WHATWG', 'forms.html#the-constraint-validation-api', 'Invalid event') }}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{ SpecName('HTML5.1', 'sec-forms.html#the-constraint-validation-api', 'Invalid event') }}</td>
   <td>{{Spec2('HTML5.1')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{ SpecName('HTML5 W3C', 'forms.html#the-constraint-validation-api', 'Invalid event') }}</td>
   <td>{{Spec2('HTML5 W3C')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("api.HTMLInputElement.invalid_event")}}</p>

<h2 id="参见">参见</h2>

<ul>
 <li>HTML {{HtmlElement("form")}} 元素</li>
 <li>相关事件:{{Event("submit")}}</li>
 <li><a href="/en-US/docs/Web/CSS/:invalid">CSS <code>:invalid</code> 伪类</a></li>
</ul>