vuejs2 - 使用复选框 Vue.js 切换固定金额
问题描述
我是 Vue.js 的新手,并试图了解复选框的工作原理。我创建了一个带有固定总数的小应用程序。这个总数会根据复选框是选中还是未选中而更新。
如果每一项都被选中,我会将单独的固定金额添加到总数中。取消选中时,固定金额将被删除。我已经让应用程序正常工作,但我想知道我写的内容是否正确,是否有更清洁的解决方案?循环遍历多行时如何工作?
提前致谢
https://codepen.io/mjwals/pen/bGWRYbw
谢谢
编码:
<!-- Use preprocessors via the lang attribute! e.g. <template lang="pug"> -->
<template>
<div id="app">
<div class="row">
£{{ total }}
<input type="checkbox" id="1" v-model="chb1" @change="add" />
<label for="1">{{ chb1 }}</label>
<input type="checkbox" id="2" v-model="chb2" @change="minus" />
<label for="2">{{ chb2 }}</label>
</div>
</div>
</template>
<script>
export default {
data() {
return {
total: 30,
chb1: false,
chb1Amount: 10,
chb2: false,
chb2Amount: 30,
};
},
methods: {
add() {
if(this.chb1) {
return this.total = this.total + this.chb1Amount;
} else {
return this.total = this.total - this.chb1Amount;
}
},
minus() {
if(this.chb2) {
return this.total = this.total + this.chb2Amount;
} else {
return this.total = this.total - this.chb2Amount;
}
},
},
computed: {
}
};
</script>
<!-- Use preprocessors via the lang attribute! e.g. <style lang="scss"> -->
<style>
.row {
border: solid 1px #dbdbdb;
padding: 10px;
}
</style>
解决方案
推荐阅读
- angular-material - 角度材料在表中组合相同的列值,并仅显示多行的单个值
- firebase - Flutter + firestore:过滤器列表
- html - VBA中的getattribute无法比较记录集中的值
- function - PostgreSQL 函数返回类型记录显示错误
- c++ - 如何修复这个*尝试*将美元转换为人民币和韩元的 C++ 程序?
- python-3.x - 递归合并排序 - RecursionError:调用 Python 对象时超出最大递归深度
- python - 尝试从请求文本中读取 JSON 字符串时出现 UnicodeEncodeError
- c - 成功分配指针后,无法将值分配给结构指针
- android - android.permission.READ_PHONE_STATE 不会返回被调用的 ID
- highcharts - 我们如何为 highcharts-default-patterns 设置不同的颜色?