繁体   English   中英

如何根据用户输入减小变量的值?

[英]How to decrease a value of a variable based on a user input?

我通常可以在不发布问题的情况下找到问题的答案,但是我完全陷在这里,因此,我感谢您的帮助。

我正在使用Javascript和jQuery创建一个成本计算器,该计算器需要能够根据输入的许可证数量提供折扣。 我的计算器费用如下:

  • 1-49个许可=每个许可50英镑(这很简单)
  • 50-249个许可=每个许可的起价为49.90英镑,但每增加一个许可,每个许可的成本将降低0.10。 例如,50个许可的价格为每个许可49.90英镑,51个许可的价格为每个许可49.80,依此类推。
  • 250+个许可证=不计算,将显示一条消息以供呼叫。

我的问题:我创建了一个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 IE 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> 

使用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 IE 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> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM