-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.html
331 lines (327 loc) · 12.8 KB
/
form.html
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Form Input Bindings</title>
<link rel="stylesheet" href="./style.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/obsidian.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
</head>
<body>
<h1>Form Input Bindings</h1>
<h2>Basic Usage</h2>
<div id="vmode-basic">
<p class="tip">主要是要介紹<code>v-model</code>,不過他也只是語法糖。</p>
<p>有一些地雷要注意</p>
<ol>
<li>會忽略所有表單元素的value、checked、selected1特性的初始值。<br /> 會選擇Vue實例數據來作為具體的值。
<br /> 必須在Vuejs的data裡,宣告初始值。(超重要!!!)
</li>
<li>對於要求IME (如中文、日語、韓語等) (IME意為'輸入法')的語言,你會發現<code>v-model</code>不會在ime輸入中得到更新。<br />如果你也想實現更新,請使用input事件。</li>
</ol>
<h3>Text</h3>
<b>HTML</b>
<pre><code class="html">< input v-model="message" placeholder="edit me" >
< p >Message is: { { message } }< /p ></code></pre>
<b>Javascript</b>
<pre><code class="javascript">data: {
message: '{{ message }}'
}</code></pre>
<b>Result</b>
<input v-model="message" placeholder="edit me">
<p>Message is: {{ message }}</p>
<h3>Multiline text</h3>
<b>HTML</b>
<pre><code class="html">< span >Multiline message is:< /span >
< p style="white-space: pre-line" >{ { message } }< /p >
< br >
< textarea v-model="message" placeholder="add multiple lines" >< /textarea ></code></pre>
<p>在<code>< textarea >< /textarea ></code>插值並不會動作,必須加<code>v-model</code>。</p>
<b>Javascript</b>
<pre><code class="javascript">data: {
message: '{{ message }}'
}</code></pre>
<b>Result</b>
<br>
<span>Multiline message is:</span>
<p style="white-space: pre-line">{{ message }}</p>
<textarea v-model="message" placeholder="add multiple lines"></textarea>
<h3>Checkbox</h3>
<p>當作開關: true | false</p>
<b>HTML</b>
<pre><code class="html">< input type="checkbox" id="checkbox" v-model="checked" >
< label for="checkbox" >{ { checked } }< /label ></code></pre>
<b>Javascript</b>
<pre><code class="javascript">data: {
checked: {{ checked }}
}</code></pre>
<b>Result</b><br />
<input type="checkbox" id="checkbox" v-model="checked">
<label for="checkbox">{{ checked }}</label>
<p>當作多選: 成為新陣列</p>
<b>HTML</b>
<pre><code class="html">< div id='example-3' >
< input type="checkbox" id="jack" value="Jack" v-model="checkedNames" >
< label for="jack" >Jack< /label >
< input type="checkbox" id="john" value="John" v-model="checkedNames" >
< label for="john" >John< /label >
< input type="checkbox" id="mike" value="Mike" v-model="checkedNames" >
< label for="mike" >Mike< /label >
< br >
< span >Checked names: { { checkedNames } }< /span >
< div ></code></pre>
<b>Javascript</b>
<pre><code class="javascript">data: {
checkedNames: {{ checkedNames }}
}</code></pre>
<b>Result</b>
<!-- <div id='example-3'> -->
<input type="checkbox" id="jack" value="Jack" v-model="checkedNames">
<label for="jack">Jack</label>
<input type="checkbox" id="john" value="John" v-model="checkedNames">
<label for="john">John</label>
<input type="checkbox" id="mike" value="Mike" v-model="checkedNames">
<label for="mike">Mike</label>
<br>
<span>Checked names: {{ checkedNames }}</span>
<!-- <div> -->
<h3>Radio</h3>
<b>HTML</b>
<pre><code class="html">< input type="radio" id="one" value="One" v-model="picked" >
< label for="one" >One< /label >
< br >
< input type="radio" id="two" value="Two" v-model="picked" >
< label for="two" >Two< /label >
< br >
< span >Picked: { { picked } }< /span ></code></pre>
<b>Javascript</b>
<pre><code class="javascript">data: {
picked: '{{ picked }}'
}</code></pre>
<b>Result</b><br />
<input type="radio" id="one" value="One" v-model="picked">
<label for="one">One</label>
<br>
<input type="radio" id="two" value="Two" v-model="picked">
<label for="two">Two</label>
<br>
<span>Picked: {{ picked }}</span>
<h3>Select</h3>
<b>HTML</b>
<pre><code class="html">< select v-model="selected1" >
< option disabled value="" >Please select one< /option >
< option >A< /option >
< option >B< /option >
< option >C< /option >
< /select >
< span >Selected: { { selected1 } }< /span ></code></pre>
<p class="tip">如果 <code>v-model</code> 的初始值沒有任何匹配的選項,
< select>元素就會以「未選擇」的狀態渲染。<br /> 在iOS中,這樣使用者將無法選擇第一個選項,因為這樣的情況下,iOS不會引發change事件。
<br /> 因此,像以上提供 <code>disabled</code> 選項是建議的做法。</p>
<b>Javascript</b>
<pre><code class="javascript">data: {
selected1: '{{ selected1 }}'
}</code></pre>
<b>Result</b><br />
<select v-model="selected1">
<option disabled value="">Please select one</option>
<option>A</option>
<option>B</option>
<option>C</option>
</select>
<span>Selected: {{ selected1 }}</span>
<p><b>複選選單</b></p>
<b>HTML</b>
<pre><code class="html">< select v-model="selected2" multiple >
< option >A< /option >
< option >B< /option >
< option >C< /option >
< /select >
< br >
< span >Selected: { { selected2 } }< /span ></code></pre>
<b>Javascript</b>
<pre><code class="javascript">data: {
selected2: '{{ selected2 }}'
}</code></pre>
<b>Result</b><br />
<select v-model="selected2" multiple>
<option>A</option>
<option>B</option>
<option>C</option>
</select>
<br>
<span>Selected: {{ selected2 }}</span>
<p><b>Key-value的選項</b></p>
<b>HTML</b>
<pre><code class="html">< select v-model="selected1" >
< option v-for="option in options" v-bind:value="option.value" >
{ { option.text } }
< /option >
< /select >
< span >Selected: { { selected1 } }< /span ></code></pre>
<b>Javascript</b>
<pre><code class="javascript">data: {
selected1: '{{ selected1 }}',
options: [
{ text: 'One', value: 'A' },
{ text: 'Two', value: 'B' },
{ text: 'Three', value: 'C' }
]
}</code></pre>
<b>Result</b><br />
<select v-model="selected1">
<option v-for="option in options" v-bind:value="option.value">
{{ option.text }}
</option>
</select>
<span>Selected: {{ selected1 }}</span>
</div>
<h2>Value Bindings</h2>
<p>可以看成一種使用者可以操作的<code>if</code>判斷式</p>
<p><code>type=radio</code>, <code>checkbox</code>的<code>input</code>,還有<code>select</code>的<code>option</code><br /> 它們的值都是static string。選上了就將<code>html</code>裡的值,賦值到<code>Javascrit</code></p>
<div id="vmode-value-bindings">
<b>HTML</b>
<pre><code class="html">< !-- `picked` is a string "a" when checked -- >
< input type="radio" v-model="picked" value="a" >
< !-- `toggle` is either true or false -- >
< input type="checkbox" v-model="toggle" >
< !-- `selected1` is a string "abc" when selected1 -- >
< select v-model="selected1" >
< option value="abc" >ABC< /option >
< /select ></code></pre>
<b>Result</b>
<!-- `picked` is a string "a" when checked -->
<input type="radio" v-model="picked" value="a">
<label>{{picked}}</label>
<!-- `toggle` is either true or false -->
<input type="checkbox" v-model="toggle">
<label>{{toggle}}</label>
<!-- `selected1` is a string "abc" when selected1 -->
<select v-model="selected1"><option value="abc">ABC</option></select>
<label>{{selected1}}</label>
<h3>Checkbox</h3>
<b>HTML</b>
<pre><code class="html">< input
type="checkbox"
v-model="toggle"
v-bind:true-value="a"
v-bind:false-value="b" ></code></pre>
<p>// when checked:<br /> vm.toggle === vm.a<br /> // when unchecked:<br /> vm.toggle === vm.b
</p>
<b>Javascript</b>
<pre><code class="javascript">data: {
toggle: {{toggle}},
a: {{a}}, //static string
b: {{b}} //static string
}</code></pre>
<b>Result</b><br />
<input type="checkbox" v-model="toggle" v-bind:true-value="a" v-bind:false-value="b">
<h3>Radio</h3>
<b>HTML</b>
<pre><code class="html">< input type="radio" v-model="picked" v-bind:value="c" ></code></pre>
<p>// when checked: vm.picked === vm.c</p>
<b>Javascript</b>
<pre><code class="javascript">data: {
picked: '{{picked}}',
c: '{{c}}', //static string
d: '{{d}}' //static string
}</code></pre>
<b>Result</b><br />
<input type="radio" v-model="picked" v-bind:value="c">
<input type="radio" v-model="picked" v-bind:value="d">
<h3>Select Options</h3>
<b>HTML</b>
<pre><code class="html">< select v-model="selected2" >
< !-- inline object literal -- >
< option v-bind:value="{ number: 123 }" >123< /option >
< /select ></code></pre>
<p>// when selected2:<br /> typeof vm.selected2 // => 'object'<br /> vm.selected2.number // => 123</p>
<b>Javascript</b>
<pre><code class="javascript">data: {
selected2: {{selected2}}
}</code></pre>
<b>Result</b>
<select v-model="selected2">
<!-- inline object literal -->
<option v-bind:value="{ number: 111 }">111</option>
<option v-bind:value="{ number: 222 }">222</option>
<option v-bind:value="{ number: 333 }">333</option>
<option v-bind:value="{ number: 123 }">123</option>
</select>
<h2>Modifiers</h2>
<h3><code>.lazy</code></h3>
<p>讓修改值的行為,在失去<code>focus</code>時執行,而不是像一般的情況在<code>change</code>時執行。</p>
<b>HTML</b>
<pre><code class="html">< !-- synced after "change" instead of "input" -->
< input v-model.lazy="msg" ></code></pre>
<b>Javascript</b>
<pre><code class="javascript">data: {
msg: '{{msg}}'
}</code></pre>
<b>Result</b><br />
<!-- synced after "change" instead of "input" -->
有加<code>lazy</code><input v-model.lazy="msg"><br /> 沒加
<code>lazy</code><input v-model="msg">
<h3><code>.number</code></h3>
<p>轉態成數字的變數型態</p>
<b>HTML</b>
<pre><code class="html">< input v-model.number="age" type="number" ></code></pre>
<b>Javascript</b>
<pre><code class="javascript">data: {
age: {{age}} //is a {{typeof(age)}}
}</code></pre>
<b>Result</b><br /> 有加
<code>number</code><input v-model.number="age" type="number"><br /> 沒加
<code>number</code><input v-model="age" type="number">
<h3><code>.trim</code></h3>
<b>HTML</b>
<pre><code class="html">< input v-model.trim="msg" ></code></pre>
<b>Javascript</b>
<pre><code class="javascript">data: {
msg: '{{msg}}'
}</code></pre>
<b>Result</b><br /> 有加
<code>trim</code><input v-model.trim="msg"><br /> 沒加
<code>trim</code><input v-model="msg">
<h2><code>v-model</code> with Components</h2>
<p>直接看<a href="https://vuefe.cn/v2/guide/components.html#自定义事件">組件的自定義事件</a></p>
</div>
<script src="https://unpkg.com/vue"></script>
<script type="text/javascript">
var vmode_basic = new Vue ({
el: '#vmode-basic',
data: {
message: 'this is message',
checked: false,
checkedNames: [],
picked: '',
selected1: '',
selected2: [],
options: [
{ text: 'One', value: 'A' },
{ text: 'Two', value: 'B' },
{ text: 'Three', value: 'C' }
]
}
})
var vmode_value_bindings = new Vue ({
el: '#vmode-value-bindings',
data: {
toggle: true,
a: true,
b: false,
picked: 'c',
c: 'c',
d: 'd',
selected1: 'qwer',
selected2: {number: 123},
msg: 'msg',
age: 0
}
})
</script>
</body>
</html>