Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue 3 Forms

Vue 3 Forms

In this section we introduce the use of Vue with forms.

We can use the v-model directive on form ,

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: