目录
v-model双向绑定,既输入框的value改变,对应的message对象值也会改变,修改message的值,input的value也会随之改变。无论改变那个值,另外一个值都会变化。
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <div id="app">
- <input type="text" v-model="message">{{message}}
- div>
- <script src="../js/vue.js">script>
- <script>
- const app = new Vue({
- el: '#app',
- data: {
- message: "hello"
- }
- })
- script>
- body>
- html>
先来一个demo实现不使用v-model实现双向绑定。
实现双向绑定需要是用v-bind和v-on,使用v-bind给input的value绑定message对象,此时message对象改变,input的值也会改变。但是改变input的value并不会改变message的值,此时需要一个v-on绑定一个方法,监听事件,当input的值改变的时候,将最新的值赋值给message对象。获取事件对象,target获取监听的对象dom,value获取最新的值。v-model = v-bind + v-on
$event
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <div id="app">
-
-
- <input type="text" :value="message" @input="changeValue">{{message}}
- div>
- <script src="../js/vue.js">script>
- <script>
- const app = new Vue({
- el: '#app',
- data: {
- message: "hello world"
- },
- methods: {
- changeValue(event){
- console.log("值改变了");
- this.message = event.target.value
- }
- }
- })
- script>
- body>
- html>
radio单选框的属性是互斥的,如果使用v-model,可以不使用name就可以互斥。
v-model绑定`sex`属性,初始值为“男”,选择女后`sex`属性变成“女”,因为此时是双向绑定。
- <div id="app">
-
- <label for="male">
- <input type="radio" id="male" name="sex" value="男" v-model="sex">男
- label>
- <label for="female">
- <input type="radio" id="female" name="sex" value="女" v-model="sex">女
- label>
- <div>你选择的性别是:{{sex}}div>
-
- div>
- <script src="../js/vue.js">script>
- <script>
- const app = new Vue({
- el:"#app",
- data:{
- message:"zzz",
- sex:"男"
- },
-
- })
- script>
checkbox可以结合v-model做单选框,也可以多选框。
checkbox结合v-model实现单选框,定义变量初始化为,点击checkbox的值为,也是。isAgree
false
true
isAgree
true
checkbox结合v-model实现多选框,定义数组对象,用于存放爱好,将与checkbox对象双向绑定,此时选中,一个多选框,就多一个true,就添加一个对象。hobbies
hobbies
hhobbies
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <div id="app">
-
- <h2>单选框h2>
- <label for="agree">
- <input type="checkbox" id="agree" v-model="isAgree">同意协议
- label>
- <h3>您的选选择是:{{isAgree}}h3>
- <button :disabled="!isAgree">下一步button>
- div>
- <script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js">script>
- <script>
- const app = new Vue({
- el: '#app',
- data: {
- isAgree: true
-
- }
- })
- script>
- body>
- html>
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <div id="app">
-
- <h2>多选框h2>
-
- <input type="checkbox" name="hobby" value="篮球" v-model="hobbies">篮球
- <input type="checkbox" name="hobby" value="足球" v-model="hobbies">足球
- <input type="checkbox" name="hobby" value="羽毛球" v-model="hobbies">羽毛球
- <input type="checkbox" name="hobby" value="乒乓球" v-model="hobbies">乒乓球
- <h2>你的爱好是:{{hobbies}}h2>
- div>
- <script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js">script>
- <script>
- const app = new Vue({
- el: '#app',
- data: {
- hobbies: []
-
- }
- })
- script>
- body>
- html>
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Titletitle>
- head>
- <body>
- <div id="app">
-
- <h2>多选框h2>
- <label :for="item" v-for="(item,index) in hhobbies" :key="index">
- <input type="checkbox" name="hobby" :value="item" :id="item" v-model="hobbies">{{item}}
- label>
- <h2>你的爱好是:{{hobbies}}h2>
- div>
- <script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js">script>
- <script>
- const app = new Vue({
- el: '#app',
- data: {
- hobbies: [],
- hhobbies: ["篮球","足球","乒乓球","羽毛球"]
-
- }
- })
- script>
- body>
- html>
v-model结合select可以单选也可以多选。
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>v-model结合select类型title>
- head>
- <body>
- <div id="app">
-
- <select name="fruit" v-model="fruit">
- <option value="苹果">苹果option>
- <option value="香蕉">香蕉option>
- <option value="西瓜">西瓜option>
- select>
- <h2>你选择的水果是:{{fruit}}h2>
-
-
- <select name="fruits" v-model="fruits" multiple>
- <option value="苹果">苹果option>
- <option value="香蕉">香蕉option>
- <option value="西瓜">西瓜option>
- select>
- <h2>你选择的水果是:{{fruits}}h2>
- div>
- <script src="../js/vue.js">script>
- <script>
- const app = new Vue({
- el:"#app",
- data:{
- fruit:"苹果",
- fruits:[]
- },
- })
- script>
- body>
lazy
:默认情况下是实时更新数据,加上,从输入框失去焦点,按下enter都会更新数据
number
:默认是字符串类型,使用复制为数字类型
trim
:用于自动过滤用户输入的首尾空白字符
- html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <meta http-equiv="X-UA-Compatible" content="ie=edge">
- <title>v-model修饰符title>
- head>
- <body>
- <div id="app">
- <h2>v-model修饰符h2>
- <h3>lazy,默认情况是实时更新数据,加上lazy,从输入框失去焦点,按下enter都会更新数据h3>
- <input type="text" v-model.lazy="message">
- <div>{{message}}div>
- <h3>修饰符number,默认是string类型,使用number赋值为number类型h3>
- <input type="number" v-model.number="age">
- <div>{{age}}--{{typeof age}}div>
- <h3>修饰符trim:去空格h3>
- <input type="text" v-model.trim="name">
-
- div>
- <script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js">script>
- <script>
- const app = new Vue({
- el:"#app",
- data:{
- message:"zzz",
- age:18,
- name:"ttt"
- },
-
- })
- script>
- body>
- html>