Home > Web Front-end > Vue.js > body text

How to use v-model to implement two-way binding of forms in Vue

WBOY
Release: 2023-06-11 10:19:40
Original
3248 people have browsed it

Vue is a popular and easy-to-learn front-end framework. Its two-way binding is very convenient in form processing. In Vue, use the v-model directive to achieve two-way binding between form elements and Vue component data attributes. The following will introduce in detail how to use v-model to implement two-way binding of forms in Vue.

  1. Understanding the v-model directive

The v-model directive is one of the important directives for two-way data binding in Vue. v-model is used to establish a two-way binding relationship between form input and application state. It can monitor changes in the input box, and can also reflect changes in component properties to the input box. In form processing, two-way binding can simplify the form processing process and make data easier to maintain and manage.

  1. Use v-model to process text boxes

v-model can handle most form elements, the most common of which is the text box. Using v-model to process text boxes is very simple. You only need to add the v-model directive to the text box element and bind it to the data attribute of the Vue component. For example, the following code demonstrates how to use v-model to handle a text box:

<template>
   <div>
       <label>输入名称:</label>
       <input v-model="name">
       <p>您输入的名称是:{{name}}</p>
   </div>
</template>
<script>
   export default {
       data() {
           return {
               name: ''
           }
       }
   }
</script>
Copy after login

In the above code, we can see that the v-model directive is added to the input element and connects it with the Vue component The name attribute performs two-way binding. In the Vue component, we define a name attribute to store the value of the input box. At the same time, the template string {{name}} is used in the p element to display the name entered by the user.

  1. Handling radio buttons and check boxes

In Vue, you can easily handle radio buttons and check boxes using v-model. For the radio button, we can use the v-model directive to define a Boolean attribute in the component for two-way binding with the checked attribute of the radio button. For check boxes, we can use the v-model directive to define an array type attribute in the component for two-way binding with the value attribute of the check box. The following code demonstrates how to handle radio buttons and check boxes:

<template>
   <div>
       <label>请选择性别:</label>
       <input type="radio" v-model="gender" value="male">男
       <input type="radio" v-model="gender" value="female">女
       <p>您选择的性别是:{{gender}}</p>
 
       <label>请选择您的爱好:</label>
       <input type="checkbox" v-model="hobbies" value="reading">阅读
       <input type="checkbox" v-model="hobbies" value="traveling">旅行
       <input type="checkbox" v-model="hobbies" value="music">音乐
       <p>您选择的爱好是:{{hobbies.join(',')}}</p>
   </div>
</template>
<script>
   export default {
       data() {
           return {
               gender: '',
               hobbies: []
           }
       }
   }
</script>
Copy after login

In the above code, we can see that for radio buttons and check boxes, we only need to use the v-model directive to They can be bidirectionally bound to the data properties of the Vue component. In the Vue component, we define two attributes, gender and hobbies, to store the gender and hobbies selected by the user. Use {{gender}} to display the gender selected by the user. When displaying hobbies, you need to use the join method of the array to splice the values ​​​​of multiple selected check boxes into a string for easy display.

  1. Processing the drop-down list box

Using the v-model directive in Vue to bidirectionally bind the drop-down list box is also very simple. We only need to add the v-model directive to the select element and bidirectionally bind it to the data attribute of the Vue component. In the Vue component, we need to define a property to store the value of the currently selected item in the drop-down box. The following code demonstrates how to use v-model to handle drop-down list boxes:

<template>
   <div>
       <label>请选择您所在的城市:</label>
       <select v-model="city">
           <option value="beijing">北京</option>
           <option value="shanghai">上海</option>
           <option value="guangzhou">广州</option>
           <option value="shenzhen">深圳</option>
       </select>
       <p>您所在的城市是:{{city}}</p>
   </div>
</template>
<script>
   export default {
       data() {
           return {
               city: ''
           }
       }
   }
</script>
Copy after login

In the above code, we can see that the v-model directive is added to the select element and connects it with the The city attribute performs two-way binding. In the Vue component, the city property is used to store the value of the currently selected item in the drop-down list box. Finally, use {{city}} to display the city selected by the user.

Summary

This article introduces how to use v-model to implement two-way binding of forms in Vue. In Vue, two-way binding of various form elements can be easily handled using the v-model directive. We can use the v-model directive to bind form elements to Vue component properties, and automatically update component property values ​​while entering the form. This method simplifies the process for developers to process form data and improves development efficiency.

The above is the detailed content of How to use v-model to implement two-way binding of forms in Vue. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!