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

Vue.js Forms

Vue.js Forms

In this section we introduce the application of Vue.js to forms.

You can use the v-model directive to create two-way data binding on form control elements.

v-model automatically selects the correct method to update the element based on the control type.

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

The message is: {{ message }}

p

<

p

textarea element:

p

<p style=">

white-space: pre

"

{{ message2 }}

p

<

textarea

v-model

=

"

message2

"

placeholder

=

"

Multiline text input...

"

textarea

div

<

script

new Vue({ el: '#app', data: { message: 'Runoob', message2: 'Runoob Tutorial\r\nhttp://www.runoob.com' } })

script

Try it »

Checkbox

If a checkbox is a single logical value; if there are multiple, they are bound 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

new Vue({ el: '#app', data: { checked : false, checkedNames: [] } })

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

new Vue({ el: '#app', data: { picked : 'Runoob' } })

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="fruit">
<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

new Vue({ el: '#app', data: { selected: '' } })

script

Try it »

Selecting Runoob, the output effect is as follows:


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: