使用keyup实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../lib/vue.js"></script>
</head>
<body>
<div id="app">
<input type="text" v-model="firstname" @keyup="getFullname">+
<input type="text" v-model="lastname" @keyup="getFullname">=
<input type="text" v-model="fullname">
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
firstname:"",
lastname:"",
fullname:""
},
methods: {
getFullname(){
this.fullname=this.firstname+this.lastname
}
},
})
</script>
</body>
</html>
使用watch监听文本框数据的变化
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../lib/vue.js"></script>
</head>
<body>
<div id="app">
<input type="text" v-model="firstname">+
<input type="text" v-model="lastname">=
<input type="text" v-model="fullname" style="width: 100%;">
</div>
<script>
var vm = new Vue({
el: "#app",
data: {
firstname: "",
lastname: "",
fullname: ""
},
methods: {},
watch: {
'firstname': function (newVal) {
this.fullname = newVal + this.lastname
},
'lastname': function (newVal) {
this.fullname = this.firstname + newVal
}
}
})
</script>
</body>
</html>