Fetching contributors…
Cannot retrieve contributors at this time
386 lines (327 sloc) 9.33 KB
title type order
Form Input Bindings

Basic Usage

You can use the v-model directive to create two-way data bindings on form input, textarea, and select 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.

`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.

For languages that require an [IME]( (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.


<input v-model="message" placeholder="edit me">
<p>Message is: {{ message }}</p>

{% raw %}

Message is: {{ message }}

<script> new Vue({ el: '#example-1', data: { message: '' } }) </script> {% endraw %}

Multiline text

<span>Multiline message is:</span>
<p style="white-space: pre-line;">{{ message }}</p>
<textarea v-model="message" placeholder="add multiple lines"></textarea>

{% raw %}

Multiline message is:

{{ message }}

<textarea v-model="message" placeholder="add multiple lines"></textarea>
<script> new Vue({ el: '#example-textarea', data: { message: '' } }) </script> {% endraw %}

{% raw %}

Interpolation on textareas (<textarea>{{text}}</textarea>) won't work. Use v-model instead.

{% endraw %}


Single checkbox, boolean value:

<input type="checkbox" id="checkbox" v-model="checked">
<label for="checkbox">{{ checked }}</label>

{% raw %}

{{ checked }}
<script> new Vue({ el: '#example-2', data: { checked: false } }) </script> {% endraw %}

Multiple checkboxes, bound to the same Array:

<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>
  <span>Checked names: {{ checkedNames }}</span>
new Vue({
  el: '#example-3',
  data: {
    checkedNames: []

{% raw %}

Jack John Mike
Checked names: {{ checkedNames }}
<script> new Vue({ el: '#example-3', data: { checkedNames: [] } }) </script> {% endraw %}


<input type="radio" id="one" value="One" v-model="picked">
<label for="one">One</label>
<input type="radio" id="two" value="Two" v-model="picked">
<label for="two">Two</label>
<span>Picked: {{ picked }}</span>

{% raw %}

Picked: {{ picked }}
<script> new Vue({ el: '#example-4', data: { picked: '' } }) </script> {% endraw %}


Single select:

<select v-model="selected">
  <option disabled value="">Please select one</option>
<span>Selected: {{ selected }}</span>
new Vue({
  el: '...',
  data: {
    selected: ''

{% raw %}

Please select one A B C Selected: {{ selected }}
<script> new Vue({ el: '#example-5', data: { selected: '' } }) </script> {% endraw %}

If the initial value of your `v-model` expression does not match any of the options, the `` 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. Multiple select (bound to Array): <select v-model="selected" multiple> <option>A</option> <option>B</option> <option>C</option> </select> <br> <span>Selected: {{ selected }}</span> {% raw %} A B C
Selected: {{ selected }} <script> new Vue({ el: '#example-6', data: { selected: [] } }) </script> {% endraw %}

Dynamic options rendered with v-for:

<select v-model="selected">
  <option v-for="option in options" v-bind:value="option.value">
    {{ option.text }}
<span>Selected: {{ selected }}</span>
new Vue({
  el: '...',
  data: {
    selected: 'A',
    options: [
      { text: 'One', value: 'A' },
      { text: 'Two', value: 'B' },
      { text: 'Three', value: 'C' }

{% raw %}

{{ option.text }} Selected: {{ selected }}
<script> new Vue({ el: '#example-7', data: { selected: 'A', options: [ { text: 'One', value: 'A' }, { text: 'Two', value: 'B' }, { text: 'Three', value: 'C' } ] } }) </script> {% endraw %}

Value Bindings

For radio, checkbox and select options, the v-model binding values are usually static strings (or booleans for checkbox):

<!-- `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">

<!-- `selected` is a string "abc" when the first option is selected -->
<select v-model="selected">
  <option value="abc">ABC</option>

But sometimes we may want to bind the value to a dynamic property on the Vue instance. We can use v-bind to achieve that. In addition, using v-bind allows us to bind the input value to non-string values.


// when checked:
vm.toggle === 'yes'
// when unchecked:
vm.toggle === 'no'

The `true-value` and `false-value` attributes don't affect the input's `value` attribute, because browsers don't include unchecked boxes in form submissions. To guarantee that one of two values is submitted in a form (e.g. "yes" or "no"), use radio inputs instead.


<input type="radio" v-model="pick" v-bind:value="a">
// when checked:
vm.pick === vm.a

Select Options

<select v-model="selected">
  <!-- inline object literal -->
  <option v-bind:value="{ number: 123 }">123</option>
// when selected:
typeof vm.selected // => 'object'
vm.selected.number // => 123



By default, v-model syncs the input with the data after each input event (with the exception of IME composition as stated above). You can add the lazy modifier to instead sync after change events:

<!-- synced after "change" instead of "input" -->
<input v-model.lazy="msg" >


If you want user input to be automatically typecast as a number, you can add the number modifier to your v-model managed inputs:

<input v-model.number="age" type="number">

This is often useful, because even with type="number", the value of HTML input elements always returns a string. If the value cannot be parsed with parseFloat(), then the original value is returned.


If you want user input to be trimmed automatically, you can add the trim modifier to your v-model managed inputs:

<input v-model.trim="msg">

v-model with Components

If you're not yet familiar with Vue's components, you can skip this for now.

HTML's built-in input types won't always meet your needs. Fortunately, Vue components allow you to build reusable inputs with completely customized behavior. These inputs even work with v-model! To learn more, read about custom inputs in the Components guide.