我通常可以在没有发帖的情况下找到问题的答案,但我完全被困在这里,所以我感谢任何帮助。
我正在使用Javascript& amp;创建一个成本计算器jQuery需要能够根据输入的许可证数量给出折扣。我的计算器费用如下:
我的问题:我创建了一个LicenceCost变量来存储每个许可证的成本,我可以用它来计算。如何设置此变量以考虑每个49以上许可证的0.10折扣?
我读过的所有文章都建议使用多个if ... else语句,我认为这些语句在我的案例中效果不错,因为我有200个用于一个小计算器。我可能会以错误的方式解决这个问题,所以如果我需要采取不同的方法,我会接受我的想法,我会很高兴再次开始!
感谢您提供任何帮助,我在下方附上了一个片段供您参考。
注意:费用计算器没有用于任何付款,只是为了说明,所以我不担心这个客户端。
$('#NumberOfLicencesInput').bind('input', function() {
// Get number of selected licences
var SelectedLicences = document.getElementById('NumberOfLicencesInput').value;
// Output number of selected licences
document.getElementById("NumberOfLicencesOutput").innerHTML = SelectedLicences;
// if selected number of licences is less than 50 then the licence cost is 50 and the total cost is calculated
if (SelectedLicences < 50) {
LicenceCost = 50;
TotalCost = LicenceCost * SelectedLicences;
document.getElementById("TotalCost").innerHTML = TotalCost
}
// if selected number of licences is more than 49 then the licence cost should starts at 49.90 but should decrease by 0.10 for each additional licence I.E 50 = 49.90, 51 = 49.80 etc...
else if (SelectedLicences > 49 && SelectedLicences < 250) {
LicenceCost = 49.90;
TotalCost = LicenceCost * SelectedLicences;
document.getElementById("TotalCost").innerHTML = TotalCost
}
// if 250 or above display a message to call
else {
document.getElementById("TotalCost").innerHTML = "Please Call";
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" id="NumberOfLicencesInput"></input>
<div>
<p>
Number of Licences:
<b id="NumberOfLicencesOutput"></b>
</p>
<p>
Total Cost:
<b id="TotalCost"></b>
</p>
</div>
答案 0 :(得分:0)
使用LicenceCost = 50.00 - (SelectedLicences-49)*.10;
计算许可证成本。许可证数量大于49。
如下面的代码段所示。
$('#NumberOfLicencesInput').bind('input', function() {
// Get number of selected licences
var SelectedLicences = document.getElementById('NumberOfLicencesInput').value;
// Output number of selected licences
document.getElementById("NumberOfLicencesOutput").innerHTML = SelectedLicences;
// if selected number of licences is less than 50 then the licence cost is 50 and the total cost is calculated
if (SelectedLicences < 50) {
LicenceCost = 50;
TotalCost = LicenceCost * SelectedLicences;
document.getElementById("TotalCost").innerHTML = TotalCost
}
// if selected number of licences is more than 49 then the licence cost should starts at 49.90 but should decrease by 0.10 for each additional licence I.E 50 = 49.90, 51 = 49.80 etc...
else if (SelectedLicences > 49 && SelectedLicences < 250) {
// this does the trick
LicenceCost = 50.00 - (SelectedLicences-49)*.10;
console.log(LicenceCost); // just to log value to console
TotalCost = LicenceCost * SelectedLicences;
document.getElementById("TotalCost").innerHTML = TotalCost
}
// if 250 or above display a message to call
else {
document.getElementById("TotalCost").innerHTML = "Please Call";
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" id="NumberOfLicencesInput"></input>
<div>
<p>
Number of Licences:
<b id="NumberOfLicencesOutput"></b>
</p>
<p>
Total Cost:
<b id="TotalCost"></b>
</p>
</div>