ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
[TOC] >[success] # 表单中双向绑定指令的使用 >[success] ## input的双向数据绑定 下面的代码中,**输入框的值改变后,页面上的文字也会改变,文字改变后,输入框中的值也会改变** ,这就是 **双向数据绑定** ,如果用 **input框的 v-model** ,就可以不用写**value** 属性了,直接用它就可以了。 **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>表单中双向绑定指令的使用</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return{ message: 'hello' } }, template: ` <div> {{message}} <input v-model="message"/> </div> ` }) const vm = app.mount('#root') </script> </html> ~~~ >[success] ## textarea的双向数据绑定 向以前 **textarea** 都要用 **双标签** 来使用,像这样: ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>表单中双向绑定指令的使用</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ template: ` <textarea> 123 </textarea> ` }) const vm = app.mount('#root') </script> </html> ~~~ 现在,在 **vue**中可以实现 **双向数据绑定** 了,而且只需要 **单标签** 的形式就可以使用。如下: ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>表单中双向绑定指令的使用</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return{ message: 'hello' } }, template: ` <textarea v-model="message"/> ` }) const vm = app.mount('#root') </script> </html> ~~~ >[success] ## checkbox的双向数据绑定 可以用 **input** 类型改为 **checkbox** ,然后绑定一个 **布尔值的变量** 即可实现 **双向数据绑定** ,代码如下: 1. **单个 checkbox** **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>表单中双向绑定指令的使用</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return{ message: false } }, template: ` {{message}} <input type="checkbox" v-model="message"/> ` }) const vm = app.mount('#root') </script> </html> ~~~ 2. **多个checkbox** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>表单中双向绑定指令的使用</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return{ message: [] } }, template: ` {{message}} jack<input type="checkbox" v-model="message" value="jack"/> dell<input type="checkbox" v-model="message" value="dell"/> lee<input type="checkbox" v-model="message" value="lee"/> ` }) const vm = app.mount('#root') </script> </html> ~~~ >[success] ### checkbox的自定义写法 **checkbox** 在 **选中** 以及 **取消选中**时候, 通过 **true-value** 、**false-value** 属性来 **动态更改显示的文字** 。 **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>表单中双向绑定指令的使用</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return{ message: 'hello' } }, template: ` {{message}} <input type="checkbox" v-model="message" true-value="hello" false-value="world"/> ` }) const vm = app.mount('#root') </script> </html> ~~~ >[success] ## radio的双向数据绑定 **radio** 储存值只需要用 **默认空字符串** 储存即可 ,不需要向 **checkbox** 那样用 **数组** ,因为 **radio** 只能选择一个值 。 **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>表单中双向绑定指令的使用</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return{ message: '' } }, template: ` {{message}} jack<input type="radio" v-model="message" value="jack"/> dell<input type="radio" v-model="message" value="dell"/> lee<input type="radio" v-model="message" value="lee"/> ` }) const vm = app.mount('#root') </script> </html> ~~~ >[success] ## select的双向数据绑定 1. **单选select** **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>表单中双向绑定指令的使用</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return{ message: '' } }, template: ` {{message}} <select v-model="message"> <option disable value="">请选择内容</option> <option value="A">A</option> <option value="B">B</option> <option value="C">C</option> </select> ` }) const vm = app.mount('#root') </script> </html> ~~~ 2. **多选select** **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>表单中双向绑定指令的使用</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return{ message: [], // options: [ // { // text: 'A', value: 'A' // }, // { // text: 'B', value: 'B' // }, // { // text: 'C', value: 'C' // } // ] // 数据可以写成对象的形式 options: [ { text: 'A', value: { value: 'A' } }, { text: 'B', value: { value: 'B' } }, { text: 'C', value: { value: 'C' } } ] } }, template: ` {{message}} <select v-model="message" multiple> <option v-for="item in options" :value="item.value">{{item.text}}</option> </select> ` }) const vm = app.mount('#root') </script> </html> ~~~ >[success] ## v-model指令修饰符、form表单标签修饰符 1. **lazy修饰符(懒加载修饰符)** **input** 框 **失去焦点** 时候会触发 **刷新数据的变化** 。 **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>表单中双向绑定指令的使用</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return{ message: 'hello' } }, template: ` {{message}} <input v-model.lazy="message"/> ` }) const vm = app.mount('#root') </script> </html> ~~~ 2. **number修饰符** **number修饰符可以做类型的转换** ,如果默认是 **string** 类型,输入值后 **保存到变量中的值是 number 类型** 。 **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>表单中双向绑定指令的使用</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return{ message: '123' } }, template: ` <div> {{ typeof message}} <input v-model.number="message" type="number /> </div> ` }) const vm = app.mount('#root') </script> </html> ~~~ 3. **trim修饰符** **trim修饰符的意思,就是去掉输入框前后的空格** **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>表单中双向绑定指令的使用</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> const app = Vue.createApp({ data(){ return{ message: '123' } }, template: ` <div> {{message}} <input v-model.trim="message"/> </div> ` }) const vm = app.mount('#root') </script> </html> ~~~