Vue 3 Forms
In this section we introduce the use of Vue with forms.
We can use the v-model directive on form , and and other elements to create two-way data bindings.

v-model automatically selects the correct method to update the element based on the control type.
v-model ignores the initial values of the value, checked, and selected attributes of all form elements, and instead uses the initial value declared in the data option.
Internally, v-model uses different properties and emits different events for different input elements:
- text and textarea elements use the
valueattribute and theinputevent; - checkbox and radio use the
checkedattribute and thechangeevent; - the select field uses
valueas the attribute andchangeas the event.
Input Box
The example demonstrates using v-model to achieve two-way data binding in input and textarea elements:
<div id="app">
<p>input element:
</
p
<
input
v-model
=
"
message
"
placeholder
=
"Edit me...
"
<
p
input form message is: {{ message }}
</
p
<
ptextarea element:
</
p
<
textarea
v-model
=
"
message2
"
placeholder
=
"Multiline text input...
"
</
textarea
<
ptextarea form message is:
</
p
<
p
style
=
"white-space: pre
"
{{ message2 }}
</
p
</
div
<
scriptconst app = { data() { return { message: '', message2: 'Runoob Tutorial\r\nhttps://www.runoob.com' } } } Vue.createApp(app).mount('#app')
script
Try it »
Interpolation in a textarea does not work; you need to use v-model instead:
Checkbox
A checkbox is a boolean value if single; if multiple, they bind to the same array:
Checkbox
The following example demonstrates two-way data binding for checkboxes:
<div id="app">
<p>Single checkbox:
p
<input type="checkbox" id="checkbox" v-model="checked">
<label for="checkbox">{{ checked }}
label
<
p
Multiple checkboxes:
p
<input type="checkbox" id="runoob" value="Runoob" v-model="checkedNames">
<label for="runoob" Runoob label>
<input type="checkbox" id="google" value="Google" v-model="checkedNames">
<label for="google" Google label>
<input type="checkbox" id="taobao" value="Taobao" v-model="checkedNames">
<label for="taobao" taobao label>
<br>
<span>Selected values are: {{ checkedNames }}
span
div
<
script
const app = { data() { return { checked : false, checkedNames: [] } } } Vue.createApp(app).mount('#app')
script
Try it »
The effect of checking the checkboxes in the example is shown below:
Radio Button
The following example demonstrates two-way data binding for radio buttons:
Radio Button
<div id="app">
<input type="radio" id="runoob" value="Runoob" v-model="picked">
<label for="runoob" Runoob label>
<br>
<input type="radio" id="google" value="Google" v-model="picked">
<label for="google" Google label>
<br>
<span>Selected value is: {{ picked }}
span
div
<
script
const app = { data() { return { picked : 'Runoob' } } } Vue.createApp(app).mount('#app')
script
Try it »
After selection, the effect is shown in the figure below:

select List
The following example demonstrates two-way data binding for a dropdown list:
select
<div id="app">
<select v-model="selected" name="site">
<option value="">Select a website
option
<option value="www.runoob.com" Runoob option>
<option value="www.google.com" Google option select>
<div id="output">Selected website is: {{selected}}
div
div
<
script
const app = { data() { return { selected: '' } } } Vue.createApp(app).mount('#app')
script
Try it »
Selecting Runoob, the output effect is as follows:

When multiple selections are allowed, it binds to an array:
select
<div id="app">
<select v-model="selected" name="fruit" multiple>
<option value="www.runoob.com" Runoob option>
<option value="www.google.com" Google option>
<option value="www.taobao.com" Taobao option select>
<div id="output">Selected website is: {{selected}}
div
div
<
script
const app = { data() { return { selected: '' } } } Vue.createApp(app).mount('#app')
script
Try it »
Selected Runoob and Google:

Use v-for Loop through options:
select
<div id="app" class="demo">
<select v-model="selected">
<option v-for=">option in options
"
:value
=
"
option.value
"
{{ option.text }}
option
select
<
span
Selected is: {{ selected }}
span
div
<
script
const app = { data() { return { selected: 'www.runoob.com', options: [ { text: 'Runoob', value: 'www.runoob.com' }, { text: 'Google', value: 'www.google.com' }, { text: 'Taobao', value: 'www.taobao.com' } ] } } } Vue.createApp(app).mount('#app')
script
Try it »
Selected Runoob:

Value Binding
For radio buttons, checkboxes, and select options, the value bound by v-model is usually a static string (or a boolean for checkboxes):
But sometimes we may want to bind the value to a dynamic property of the current active instance; this can be done with v-bind. In addition, using v-bind allows binding the input value to non-string types.
Checkbox:
...
// 选中时
vm.toggle === 'yes'
// Deselect
vm.toggle === 'no'
Radio:
// When selected
vm.pick === vm.a
Select option:
// When selected
typeof vm.selected // => 'object'
vm.selected.number // => 123
Modifiers
.lazy
By default, v-model synchronizes the input value with the data on the input event, but you can add the lazy modifier to switch to synchronizing on the change event:
.number
If you want to automatically convert the user's input to a Number type (returning the original value if the conversion result is NaN), you can add the number modifier to v-model to process the input:
This is often useful because even with type="number", values entered in HTML are always returned as strings.
.trim
If you want to automatically trim leading and trailing whitespace from user input, you can add the trim modifier to v-model to filter the input: