SoFunction
Updated on 2025-04-10

Vue study notes form input control binding

Form input binding

Basic usage

text

<input v-model:"msg" placeholder="edit me"/>

### Multi-line text

<textarea name="" id="" cols="30" rows="10" v-model:"msg"></textarea>

### Check box

Hobby

&lt;input type="checkbox" value="See a movie" v-model="checked"/&gt;
&lt;input type="checkbox" value="Play the game" v-model="checked"/&gt;
&lt;input type="checkbox" value="music" v-model="checked"/&gt;
{{ checked }} 

var vm=new Vue({
   el:'#app',
   date:{
    checked:[]
   }, 

Radio button

gender

&lt;input type="radio" value="male" v-model="picked"/&gt;male
&lt;input type="radio" value="female" v-model="picked"/&gt;female
{{picked}} 

var vm=new Vue({
 el:'#app',
 date:{
  picked: ''
 },

Select List

Single choice list

Household registration

 &lt;select v-model="selected"&gt;
 &lt;option disabled value=""&gt;Please select&lt;/option&gt;
 &lt;option&gt;Hebei&lt;/option&gt;
 &lt;option&gt;Shanxi&lt;/option&gt;
 &lt;option&gt;Beijing&lt;/option&gt; 
 &lt;/select&gt;
{{ selected }} 

var vm=new Vue({
 el:'#app',
 date:{
  selected:'',
 }, 

Multiple-select list (binding to an array):

<div >
 <select v-model="selected" multiple style="width: 50px">
 <option>A</option>
 <option>B</option>
 <option>C</option>
 </select>
 <br>
 <span>Selected: {{ selected }}</span>
</div> 
new Vue({
 el: '#example-6',
 data: {
 selected: []
 }
}) 

Bind value

For radio buttons, checkboxes and select list options, the value bound by v-model is usually a static string (a logical value for checkboxes):

&lt;!-- When selected,`picked` as a string "a" --&gt;
&lt;input type="radio" v-model="picked" value="a"&gt;
&lt;!-- `toggle` for true or false --&gt;
&lt;input type="checkbox" v-model="toggle"&gt;
&lt;!-- When selected,`selected` as a string "abc" --&gt;
&lt;select v-model="selected"&gt;
 &lt;option value="abc"&gt;ABC&lt;/option&gt;
&lt;/select&gt; 

Check box

&lt;input
 type="checkbox"
 v-model="toggle"
 v-bind:true-value="a"
 v-bind:false-value="b"
&gt; 

// When selected === 
// When not selected === 

Radio button

&lt;input type="radio" v-model="pick" v-bind:value="a"&gt; 

// When selected ===  

Select List Settings

&lt;select v-model="selected"&gt;
 &lt;!-- Inline object literal --&gt;
 &lt;option v-bind:value="{ number: 123 }"&gt;123&lt;/option&gt;
&lt;/select&gt; 

// When selectedtypeof  // =&gt; 'object'
 // =&gt; 123 

Modifier

.lazy

By default, v-model synchronizes the input box's value with data in the input event (except the IME section above), but you can add a modifier lazy to synchronize in the change event:

 {{msg}}
<input type="text" :"msg"/> 

.number

If you want to automatically convert the user's input value to the Number type, you can add a modifier number to v-model to process the input value:

{{num1}}
<input type="text" :"num1"/> 
{{num2}}
<input type="text" :"num2"/> 

{{num1+num2}} 

var vm=new Vue({
 date:{
  num1:1,
  num2:1
 },
}); 

.trim

If you want to automatically filter the beginning and end spaces of user input, you can add the trim modifier to filter the input on v-model:

 {{msg}}
<input type="text" :"msg"/> 

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.