Skip to content

Commit 27a4779

Browse files
committed
improvements to v-model docs
1 parent 60a0dcb commit 27a4779

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/v2/guide/forms.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ order: 10
88

99
You can use the `v-model` directive to create two-way data bindings on form input and textarea elements. It automatically picks the correct way to update the element based on the input type. Although a bit magical, `v-model` is essentially syntax sugar for updating data on user input events, plus special care for some edge cases.
1010

11-
<p class="tip">`v-model` doesn't care about the initial value provided to an input or a textarea. It will always treat the Vue instance data as the source of truth.</p>
11+
<p class="tip">`v-model` will ignore the initial `value`, `checked` or `selected` attributes found on any form elements. It will always treat the Vue instance data as the source of truth. You should declare the initial value on the JavaScript side, inside the `data` option of your component.</p>
1212

1313
<p class="tip" id="vmodel-ime-tip">For languages that require an [IME](https://en.wikipedia.org/wiki/Input_method) (Chinese, Japanese, Korean etc.), you'll notice that `v-model` doesn't get updated during IME composition. If you want to cater for these updates as well, use `input` event instead.</p>
1414

@@ -169,15 +169,25 @@ Single select:
169169

170170
``` html
171171
<select v-model="selected">
172+
<option disabled value="">Please select one</option>
172173
<option>A</option>
173174
<option>B</option>
174175
<option>C</option>
175176
</select>
176177
<span>Selected: {{ selected }}</span>
177178
```
179+
``` js
180+
new Vue({
181+
el: '...',
182+
data: {
183+
selected: ''
184+
}
185+
})
186+
```
178187
{% raw %}
179188
<div id="example-5" class="demo">
180189
<select v-model="selected">
190+
<option disabled value="">Please select one</option>
181191
<option>A</option>
182192
<option>B</option>
183193
<option>C</option>
@@ -188,12 +198,14 @@ Single select:
188198
new Vue({
189199
el: '#example-5',
190200
data: {
191-
selected: null
201+
selected: ''
192202
}
193203
})
194204
</script>
195205
{% endraw %}
196206

207+
<p class="tip">If the initial value of your `v-model` expression does not match any of the options, the `<select>` element will render in an "unselected" state. On iOS this will cause the user not being able to select the first item because iOS does not fire a change event in this case. It is therefore recommended to provide a disabled option with an empty value, as demonstrated in the example above.</p>
208+
197209
Multiple select (bound to Array):
198210

199211
``` html

0 commit comments

Comments
 (0)