Javascript 按下按钮的次数应与变量(往复)值相乘
我正在开发Angular 8应用程序。我想将按下按钮的次数乘以现有值(往复)。但它没有给我正确的结果 结果应该是=>8*3=24 但是我得到的结果是=>48 谁能帮我一下吗 我的TS代码Javascript 按下按钮的次数应与变量(往复)值相乘,javascript,html,angular,typescript,angular8,Javascript,Html,Angular,Typescript,Angular8,我正在开发Angular 8应用程序。我想将按下按钮的次数乘以现有值(往复)。但它没有给我正确的结果 结果应该是=>8*3=24 但是我得到的结果是=>48 谁能帮我一下吗 我的TS代码 quantityControl(flag) { if (flag == '+') { this.quantity = this.quantity + 1; if (this.filteredRecipe.RecipePrice === 0) { this.r
quantityControl(flag) {
if (flag == '+') {
this.quantity = this.quantity + 1;
if (this.filteredRecipe.RecipePrice === 0) {
this.recipeTotal = this.recipeTotal * this.quantity;
console.log('The price is:'+ this.recipeTotal);
}
} else {
if (this.quantity != 1) {
this.quantity = this.quantity - 1;
if (this.filteredRecipe.RecipePrice === 0) {
this.recipeTotal = this.recipeTotal * this.quantity;
}
}
}
}
<div class="u-line">
<div class="product-quantity padding-10"><span class="text-light-black fw-700 fs-16">Quantity</span>
<div class="input-group quantity">
<div class="input-group-append">
<button (click)="quantityControl('-')" class="minus-btn" type="button" name="button">
<i class="fas fa-minus"></i>
</button>
</div>
<input type="text" class="text-center" [(ngModel)]="quantity" name="quantity" value="1" style="font-size: 18px; font-weight: bold;">
<div class="input-group-prepend">
<button (click)="quantityControl('+')" class="plus-btn" type="button" name="button"><i
class="fas fa-plus"></i>
</button>
</div>
</div>
</div>
</div>
我的HTML代码
quantityControl(flag) {
if (flag == '+') {
this.quantity = this.quantity + 1;
if (this.filteredRecipe.RecipePrice === 0) {
this.recipeTotal = this.recipeTotal * this.quantity;
console.log('The price is:'+ this.recipeTotal);
}
} else {
if (this.quantity != 1) {
this.quantity = this.quantity - 1;
if (this.filteredRecipe.RecipePrice === 0) {
this.recipeTotal = this.recipeTotal * this.quantity;
}
}
}
}
<div class="u-line">
<div class="product-quantity padding-10"><span class="text-light-black fw-700 fs-16">Quantity</span>
<div class="input-group quantity">
<div class="input-group-append">
<button (click)="quantityControl('-')" class="minus-btn" type="button" name="button">
<i class="fas fa-minus"></i>
</button>
</div>
<input type="text" class="text-center" [(ngModel)]="quantity" name="quantity" value="1" style="font-size: 18px; font-weight: bold;">
<div class="input-group-prepend">
<button (click)="quantityControl('+')" class="plus-btn" type="button" name="button"><i
class="fas fa-plus"></i>
</button>
</div>
</div>
</div>
</div>
量
可能是这个函数的错误
if (this.filteredRecipe.RecipePrice === 0)
过去的代码或自己检查。问题在于“recipetotal”变量。当按钮单击一次时,其值将与它相乘。但当按下按钮两次或三次时。以前存储的“recipetotal”值已乘以。如果第一次按下此条件“this.filteredRecipe.RecipePrice==0”,则单击按钮后,“RecipeTotal”值会自动增加。请尝试使用控制台日志进行调试。最好检查else条件中的“this.quantity”是否不等于0,因为变量“recipetotal”在if条件中。它在控制台内给了我相同的答案,也可能是事件传播问题,请尝试使用jquery代码来防止它只捕获一次事件-->$(“按钮”).click(function(){$(“p”).off(“click”);};