<html>
<head>
<meta charset="utf-8">
<script src="https://cdn.bootcss.com/vue/2.4.2/vue.min.js"></script>
</head>
<body>
<div id = "computed_props">
数量 : <input type = "text" v-model = "kilometers">
单价 : <input type = "text" v-model = "meters">
金额 : <input type = "text" v-model = "meters1">
</div>
<p id="info"></p>
<script type = "text/javascript">
var vm = new Vue({
el: '#computed_props',
data: {
kilometers : 1,
meters:1,
meters1:9
},
methods: {
},
computed :{
},
watch : {
kilometers:function(val) {
if(this.meters!=0)
{
this.kilometers = val;
this.meters1 = this.meters*val;}
},
meters:function(val) {
if(this.kilometers!=0)
{
this.meters = val;
this.meters1 = this.kilometers*val;
}
},
meters1:function(val) {
if(this.kilometers!=0)
{
this.meters = val;
this.meters = val/this.kilometers;
}
else
{
this.meters = 0
}
}
}
});
// $watch 是一个实例方法
vm.$watch('kilometers', function (newValue, oldValue) {
// 这个回调将在 vm.kilometers 改变后调用
document.getElementById ("info").innerHTML = "修改前值为: " + oldValue + ",修改后值为: " + newValue;
})
</script>
</body>
</html>
1. 给每一个input 一个id值
2.获取他们三个的值
3.给单价和金额一个keydown的方法
4.分别计算赋值 就ok了啊