- Impara
-
Ecosistema
Aiuto
Tooling
Librerie Core
Novità
Risorse
- Team
- Supporta Vue
- Traduzioni
Guide
Essenziale
- Installation
- Introduction
- The Vue Instance
- Template Syntax
- Computed Properties and Watchers
- Class and Style Bindings
- Conditional Rendering
- List Rendering
- Event Handling
- Form Input Bindings
- Components
Animazioni e transizioni
- Enter/Leave & List Transitions
- State Transitions
Riusabilità e Composizione
- Mixins
- Custom Directives
- Render Functions & JSX
- Plugins
- Filters
Tooling
- Production Deployment
- Single File Components
- Unit Testing
- TypeScript Support
Scaling Up
- Routing
- State Management
- Server-Side Rendering
Internals
- Reactivity in Depth
Migrazioni
- Migration from Vue 1.x
- Migration from Vue Router 0.7.x
- Migration from Vuex 0.6.x to 1.0
Meta
- Comparison with Other Frameworks
- Join the Vue.js Community!
- Meet the Team
Form Input Bindings
Basic Usage
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.
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.
Text
<input v-model="message" placeholder="edit me"> |
Message is: {{ message }}
Multiline text
<span>Multiline message is:</span> |
{{ message }}
Interpolation on textareas (<textarea>{{text}}</textarea>
) won't work. Use v-model
instead.
Checkbox
Single checkbox, boolean value:
<input type="checkbox" id="checkbox" v-model="checked"> |
Multiple checkboxes, bound to the same Array:
<div id='example-3'> |
new Vue({ |
Checked names: {{ checkedNames }}
Radio
<input type="radio" id="one" value="One" v-model="picked"> |
Picked: {{ picked }}
Select
Single select:
<select v-model="selected"> |
new Vue({ |
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.
Multiple select (bound to Array):
<select v-model="selected" multiple> |
Selected: {{ selected }}
Dynamic options rendered with v-for
:
<select v-model="selected"> |
new Vue({ |
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 --> |
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.
Checkbox
<input |
// when checked: |
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.
Radio
<input type="radio" v-model="pick" v-bind:value="a"> |
// when checked: |
Select Options
<select v-model="selected"> |
// when selected: |
Modifiers
.lazy
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" --> |
.number
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.
.trim
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.