删除逗号并重新添加时出现问题
2021-05-26
65
Q: 流程:原来每个数字先把逗号去掉,然后相加,相加后再把逗号加回去,第一次点击总金额没问题(500,000,010),但是第二次点击就会变成(500)
new Vue({
el: "#app",
data: () => ({
goldOriginal: "10",
items: [{
goldAmout: "100,000,000"
},
{
goldAmout: "100,000,000"
},
{
goldAmout: "100,000,000"
},
{
goldAmout: "100,000,000"
},
{
goldAmout: "100,000,000"
},
],
}),
methods: {
revert() {
var goldItem = 0;
var goldTotal = 0;
for (let i in this.items) {
let goldAmout = this.items[i].goldAmout;
goldItem += parseInt(goldAmout.split(",").join(""));
goldTotal = parseInt(this.goldOriginal) + parseInt(goldItem);
this.items[i].goldAmout = 0;
}
this.goldOriginal = goldTotal.toLocaleString("en-US");
},
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.min.js"></script>
<div id="app">
<span>{{goldOriginal}}</span>
<div class="revert-all text-center" @click="revert()">click</div>
<div class="item-box" v-for="(item, i) in items" :key="i">
<div class="amout-item d-flex justify-content-center">
<div class="update-amout">{{ item.goldAmout }}</div>
</div>
<div class="amout-item d-flex justify-content-center">
<div class="update-amout">{{ item.silverAmout }}</div>
</div>
</div>
</div>
2个回答
您还需要分割您的 goldOriginal。
revert() {
var goldItem = 0;
var goldTotal = 0;
for (let i in this.items) {
let goldAmout = this.items[i].goldAmout;
goldItem += parseInt(goldAmout.split(",").join(""));
goldTotal = parseInt(this.goldOriginal.split(",").join("")) + parseInt(goldItem);
this.items[i].goldAmout = "0";
}
this.goldOriginal = goldTotal.toLocaleString("en-US");
}
我发现这有点难以理解,所以我制作了一个较短的版本
revert() {
let goldTotal = parseInt(this.goldOriginal.split(",").join(""))
for (let i in this.items) {
let parsedGold = parseInt(this.items[i].goldAmout.split(",").join(""))
goldTotal += parsedGold;
this.items[i].goldAmout = "0";
}
this.goldOriginal = goldTotal.toLocaleString("en-US");
},
Cerceis
2021-05-26
尝试一下:
- this.items[i].goldAmout = 0;
+ this.items[i].goldAmout = "0";
Chris Kao
2021-05-26