我有一个字段计算,如果结果低于 60,则需要显示最小值
I have a field calculating and need to display a minimum if the result is below 60
对不起,这显然是我第一次来这里,我只是在学习如何使用javascript。我的问题是:我有一些基本的计算来确定我们非营利组织的服务价格。t 是房间数 * 0.81。但我们每月最低为 60 美元。所以我需要知道如何将其纳入定价功能。我知道"如果x<60,那么60",只是不确定语言将如何编写。我将包括完整的js。
var listenerFieldIDs = {"roomCountID":"item4_text_1"}; //Currently the only form we are using for room count has this value set as its ID attribute.
var impactFields = ["item12_text_1","item1_text_1","item16_text_1","item18_text_1","item20_text_1"]; //Field IDs for the form that will be changed constantly.
var estimatedBottleSize = 1.5, occupancyRate = (60 / 100), collectionDuration = 365, soapOuncesRecoverable = 0.63, bottleOuncesRecoverable = 0.47,lbConversion = 0.0626, rate = 0.81;
var $ = function(id){ //Shortcut to save some typing. Instead of having to write out document.getElementById(elementID) every time I need to access an element, I can put $(elementID).property or $(elementID).method() I need more easily.
return document.getElementById(id);
}
var updateFormField = function(id,amount){ //Updates a form field when gives the element ID and the amount.
$(id).value = amount;
}
var updateForm = function(roomCount){
// This is called when all form data needs to be updated. This is generally invoked each time a keystroke in the room count field.
updateFormField(impactFields[0],calculateLbsOfSoap(roomCount).toFixed(2)); //Updating the first form field after calculating the total weight of soap in lbs.
updateFormField(impactFields[1],calculateLbsOfBottles(roomCount).toFixed(2)); //Same thing as above, but bottles/amenities.
updateFormField(impactFields[2],calculateBarsOfSoap(roomCount).toFixed(0)); //Updating the third form field after calculating the total number of distributed units.
updateFormField(impactFields[3],calculateBottles(roomCount).toFixed(0)); //Same as above, but bottles/amenities.
updateFormField(impactFields[4],("$" + calculatePrice(roomCount).toFixed(2))); //Updating price.
}
var listenForNumbers = function(event){ //This function is acting as a handler for when anything is entered into the field.
updateForm($(listenerFieldIDs["roomCountID"]).value);
}
var calculateLbsOfSoap = function (rmCnt){ // Calculate the weight of soap and return the amount.
return checkCount(rmCnt) ? 0 : ((soapOuncesRecoverable * lbConversion) * (rmCnt * occupancyRate) * collectionDuration);
}
var calculateLbsOfBottles = function (rmCnt){ // Calculate the weight of bottled amenities and return the amount.
return checkCount(rmCnt) ? 0 : ((bottleOuncesRecoverable * lbConversion) * (rmCnt * occupancyRate) * collectionDuration);
}
var calculateBarsOfSoap = function(rmCnt){ // Calculate how many bars are distributed if the room count is not 0.
return checkCount(rmCnt) ? 0 : ((calculateLbsOfSoap(rmCnt) * 16) / 3);
}
var calculateBottles = function(rmCnt){ // Calculate how many bottles are distributed if the room count is not 0.
return checkCount(rmCnt) ? 0 : (((calculateLbsOfBottles(rmCnt) * 16) / estimatedBottleSize) * (2 / 3));
}
var calculatePrice = function(rmCnt){
return checkCount(rmCnt) ? 0 : (rmCnt * rate);
}
var checkCount = function(count){ //If the count is 0 or less than 0, the number is useless so just return 0 to prevent odd results.
return (count < 0 || count == 0) ? true : false;
}
var initializeRealTimeCalcToForm = function(){
if(window.attachEvent){
$(listenerFieldIDs["roomCountID"]).attachEvent("onkeydown",listenForNumbers,false);
$(listenerFieldIDs["roomCountID"]).attachEvent("onkeyup",listenForNumbers,false);
$(listenerFieldIDs["roomCountID"]).attachEvent("onkeypress",listenForNumbers,false);
$(listenerFieldIDs["roomCountID"]).attachEvent("onchange",listenForNumbers,false);
} else{
//But if NOT IE... :-D
$(listenerFieldIDs["roomCountID"]).addEventListener("keydown",listenForNumbers,false);
$(listenerFieldIDs["roomCountID"]).addEventListener("keyup",listenForNumbers,false);
$(listenerFieldIDs["roomCountID"]).addEventListener("keypress",listenForNumbers,false);
$(listenerFieldIDs["roomCountID"]).addEventListener("change",listenForNumbers,false);
}
}
window.onload = function(){
initializeRealTimeCalcToForm();
}
如果您只想为变量 myvar
设置最小值 60 ,您可以
myvar=Math.max(60,myvar);
编辑:
然后,如果您希望 calculatePrice
返回的值至少为 60,请使用:
var calculatePrice=function(rmCnt){
return Math.max(60,checkCount(rmCnt) ? 0 : (rmCnt * rate));
}
注1:
你知道你可以声明这样的函数吗?
function calculatePrice(rmCnt){
return Math.max(60,checkCount(rmCnt) ? 0 : (rmCnt * rate));
}
它更短,这样您就可以在声明函数之前调用它!
注2:
如果您希望该值至少为 60,我不明白以下代码:
checkCount(rmCnt) ? 0
为什么是0?
相关文章:
- Html5输入属性的默认值,如最小值、最大值、大小等
- Angular JS在一行中查找最小值
- 识别滑块范围的最小值和最大值
- 求除零以外的最小值
- Javascript-如何使用函数找到数组的最小值
- 我有一个字段计算,如果结果低于 60,则需要显示最小值
- 如何在拖动后获得图表的最小值和最大值放大高图表
- 如何仅在存在最小值和最大值时才显示错误消息
- 当达到最小值和最大值时,防止在输入中显示额外的文本
- 具有固定最小值的jQuery UI滑块:显示最小范围,而不是最小绝对值
- 检查可变总成本的最小值,如果小于最小值,则显示警报消息
- Jquery 最小值和最大值显示新页面
- 是否可以在剑道UI线性仪表上仅显示最大值/最小值
- Highcharts:在箱线图中显示标签(最小值,最大值,中位数等)
- 折线图,当数据小于最小值时,在图内显示数据标签
- 我有四个html文本框.在其他文本框中显示最小值
- 动态更改标签以显示y轴最小值和最大值
- 用jquery突出显示html列的最小值
- 谷歌图表中的柱状图显示在最小值下面
- 当最后一个点在当前x轴最小值之前,下一个点在当前x轴最大值之后时,高图不显示线