v-model的使用

v-model的基本使用

v-model指令的作用:

  • 是便捷的设置和获取表单元素的值;
  • 绑定的数据会和表单元素值相关联
  • 绑定的数据和表单元素的值双向绑定
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-model</title>
</head>
<body>
<div id="app">
<!-- 键控代码38代表上上箭头 -->
<input type="text" v-model.lazy="message" @keyup.enter="GetM" @keyup.38="SetM">
<h2>{{message}}</h2>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<script>
var app = new Vue({
el:"#app",
data:{
message:"陈建",
},
methods:{
GetM:function(){
alert(this.message+"你好!");
},
SetM:function(){
this.message="老王";
}
}
})
</script>
</body>
</html>

v-model的原理

v-model其实是一个语法糖,它的背后本质上是包含两个操作:

  1. v-bind绑定一个value属性

  2. v-on指令给当前元素绑定input事件

    1
    2
    3
    <input type="text" v-model="message">
    等同于
    <input type="text" v-bind:value="message" v-on:input="message = $event.target.value">
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>

    <div id="app">
    <!--<input type="text" v-model="message">-->
    <!--<input type="text" :value="message" @input="valueChange">-->
    <input type="text" :value="message" @input="message = $event.target.value">
    <h2>{{message}}</h2>
    </div>

    <script src="../js/vue.js"></script>
    <script>
    const app = new Vue({
    el: '#app',
    data: {
    message: '你好啊'
    },
    methods: {
    valueChange(event) {
    this.message = event.target.value;
    }
    }
    })
    </script>

    </body>
    </html>

    v-model结合radio类型

    v-model结合radio类型

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    </head>
    <body>
    <div id='app'>
    <!-- 在radio中具有相同的name属性可以实现单选,
    在vue中只要v-model等于同一个数据值,也可以实现这一功能 -->
    <!-- <label>标签的作用是为鼠标用户改进了可用性,
    当用户点击<label>标签中的文本时,
    浏览器就会自动将焦点转到和该标签相关联的控件上;
    <label>标签在单选按钮和复选按钮上经常被使用,
    使用该标签后,你点击单选按钮或复选按钮的文本也是可以选中的。
    <label for="关联控件的id" form="所属表单id列表">文本内容</label>
    -->
    <label for="male">
    <input type="radio" id="male" value="男" v-model="sex">男
    </label>
    <label for="female">
    <input type="radio" id="female" value="女" v-model="sex">女
    </label>
    <h2>您选择的性别是:{{sex}}</h2>
    </div>
    <script src='https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js'></script>
    <script>
    const app = new Vue({
    el: '#app',
    data :{
    sex: ''
    },
    methods:{
    }
    })
    </script>
    </body>
    </html>

    v-model结合checkbox类型

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    </head>
    <body>
    <div id='app'>
    <!--1.checkbox单选框-->··
    <label id="agree">
    <input type="checkbox" value="同意协议" id="agree" v-model="IsAgree">同意协议
    </label>
    <h2> 您选择的是:{{IsAgree}}</h2>
    <button :disabled="!IsAgree">下一步</button>

    <!--2.checkbox多选框-->
    <input type="checkbox" value="篮球" v-model="hobbies">篮球
    <input type="checkbox" value="足球" v-model="hobbies">足球
    <input type="checkbox" value="乒乓球" v-model="hobbies">乒乓球
    <input type="checkbox" value="羽毛球" v-model="hobbies">羽毛球
    <h2> 您选择的是:</h2>
    <ul>
    <li v-for="item in hobbies">{{item}}</li>
    </ul>

    <label v-for="item in originHobbies" :for="item">
    <input type="checkbox" :value="item" :id="item" v-model="hobbies">{{item}}
    </label>
    </div>
    <script src='https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js'></script>
    <script>
    const app = new Vue({
    el: '#app',
    data :{
    IsAgree: false,
    hobbies: [],
    originHobbies: ['篮球', '足球', '乒乓球', '羽毛球', '台球', '高尔夫球']
    },
    methods:{

    }
    })
    </script>
    </body>
    </html>

    v-model结合select类型

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>

    <div id="app">
    <!--1.选择一个-->
    <select name="abc" v-model="fruit">
    <option value="苹果">苹果</option>
    <option value="香蕉">香蕉</option>
    <option value="榴莲">榴莲</option>
    <option value="葡萄">葡萄</option>
    </select>
    <h2>您选择的水果是: {{fruit}}</h2>

    <!--2.选择多个-->
    <select name="abc" v-model="fruits" multiple>
    <option value="苹果">苹果</option>
    <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: {
    message: '你好啊',
    fruit: '香蕉',
    fruits: []
    }
    })
    </script>

    </body>
    </html>

    v-model修饰符的使用

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    </head>
    <body>

    <div id="app">
    <!--1.修饰符: lazy 实现懒加载-->
    <input type="text" v-model.lazy="message">
    <h2>{{message}}</h2>


    <!--2.修饰符: number 限定输入为numberl类型-->
    <input type="number" v-model.number="age">
    <h2>{{age}}-{{typeof age}}</h2>

    <!--3.修饰符: trim 去除空格-->
    <input type="text" v-model.trim="name">
    <h2>您输入的名字:{{name}}</h2>
    </div>

    <script src="../js/vue.js"></script>
    <script>
    const app = new Vue({
    el: '#app',
    data: {
    message: '你好啊',
    age: 0,
    name: ''
    }
    })

    var age = 0
    age = '1111'
    age = '222'
    </script>

    </body>
    </html>

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!