aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/globaleventhandlers/oninvalid/index.html
blob: d787d861cebc8fafa8d5026a03679dad454670bf (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
---
title: GlobalEventHandlers.oninvalid
slug: Web/API/GlobalEventHandlers/oninvalid
tags:
  - API
  - GlobalEventHandlers
  - 事件句柄
  - 属性
  - 引用
translation_of: Web/API/GlobalEventHandlers/oninvalid
---
<div>{{ ApiRef("HTML DOM") }}</div>

<p>{{domxref("GlobalEventHandlers")}} 混合的<code><strong>oninvalid</strong></code>属性 是 {{event("Event_handlers", "event handler")}} 处理{{event("invalid")}} 事件。</p>

<p><code>invalid</code> 事件被触发当一个从属元素被勾选checked 并且与之前的选中方式不同。 有效的从属元素在表单之前被选中, 或者在 <code><a href="/en-US/docs/Learn/HTML/Forms/Form_validation">checkValidity()</a></code> 方法之后它自己的表单被调用。</p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox"><em>target</em>.oninvalid = <em>functionRef</em>;
var <em>functionRef</em> = <em>target</em>.oninvalid;
</pre>

<h3 id="值"></h3>

<p><code>functionRef</code>是一个函数名称 或者 称为 函数表达式。这个函数需要{{domxref("Event")}} 对象做为它的参数。</p>

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

<p>这个例子用一个表单说明<code>oninvalid</code>{{domxref("GlobalEventHandlers.onsubmit", "onsubmit")}} 事件 句柄。</p>

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

<pre class="brush: html">&lt;form id="form"&gt;
  &lt;p id="error" hidden&gt;Please fill out all fields.&lt;/p&gt;

  &lt;label for="city"&gt;City&lt;/label&gt;
  &lt;input type="text" id="city" required&gt;

  &lt;button type="submit"&gt;Submit&lt;/button&gt;
&lt;/form&gt;
&lt;p id="thanks" hidden&gt;Your data has been received. Thanks!&lt;/p&gt;</pre>

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

<pre class="brush: js">const form = document.getElementById('form');
const error = document.getElementById('error');
const city = document.getElementById('city');
const thanks = document.getElementById('thanks');

city.oninvalid = invalid;
form.onsubmit = submit;

function invalid(event) {
  error.removeAttribute('hidden');
}

function submit(event) {
  form.setAttribute('hidden', '');
  thanks.removeAttribute('hidden');

  // For this example, don't actually submit the form
  event.preventDefault();
}</pre>

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

<p>{{EmbedLiveSample("Example")}}</p>

<h2 id="说明">说明</h2>

<table class="spectable standard-table">
 <tbody>
  <tr>
   <th scope="col">说明</th>
   <th scope="col">状态</th>
   <th scope="col">注释</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG','#handler-oninvalid','oninvalid')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="支持的浏览器">支持的浏览器</h2>

<p>{{Compat("api.GlobalEventHandlers.oninvalid")}}</p>

<h2 id="请参阅">请参阅</h2>

<ul>
 <li>{{event("invalid")}} event</li>
 <li><a href="/en-US/docs/Web/Guide/Events/Event_handlers" title="/en-US/docs/Web/Guide/DOM/Events/Event_handlers">DOM on-event handlers</a></li>
</ul>