Javascript 我有一个字段正在计算,如果结果低于60,则需要显示最小值

Javascript 我有一个字段正在计算,如果结果低于60,则需要显示最小值,javascript,html,Javascript,Html,很抱歉,这显然是我第一次来这里,我只是在学习如何使用javascript。我的问题是:我有一些基本的计算来决定我们非营利组织的服务价格。t是房间数*0.81。但我们每个月至少有60美元。所以我需要知道如何将其纳入定价函数。我知道“如果x

很抱歉,这显然是我第一次来这里,我只是在学习如何使用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();
}
var listenerfieldid={“roomCountID”:“item4\u text\u 1”}//目前,我们用于房间计数的唯一表单将此值设置为其ID属性。
var impactFields=[“item12_text_1”、“item1_text_1”、“item16_text_1”、“item18_text_1”、“item20_text_1”]//表单的字段ID将不断更改。
var estimatedBottleSize=1.5,OccupencyRate=60/100,collectionDuration=365,soapOuncesRecoverable=0.63,BottlesRecoverable=0.47,lbConversion=0.0626,rate=0.81;
var$=函数(id){//保存某些键入的快捷方式。每次需要访问元素时,我不必写出document.getElementById(elementID),我可以更轻松地放入$(elementID).property或$(elementID).method()。
返回文档.getElementById(id);
}
var updateFormField=函数(id,amount){//在给出元素id和金额时更新表单字段。
$(id).值=金额;
}
var updateForm=函数(roomCount){
//当需要更新所有表单数据时调用此函数。通常,每次在房间计数字段中击键时都会调用此函数。
updateFormField(impactFields[0],calculateLbsOfSoap(roomCount).toFixed(2));//在计算soap的总重量(以磅为单位)后更新第一个表单字段。
updateFormField(impactFields[1],CalculatelBSOFVaults(roomCount).toFixed(2));//与上述内容相同,但瓶子/设施。
updateFormField(impactFields[2],calculateBarsOfSoap(roomCount).toFixed(0));//在计算分布式单元的总数后更新第三个表单字段。
updateFormField(impactFields[3],calculateBottles(roomCount).toFixed(0));//与上面相同,但瓶子/便利设施。
updateFormField(impactFields[4],(“$”+calculatePrice(roomCount).toFixed(2));//更新价格。
}
var listenForNumbers=function(event){//此函数在字段中输入任何内容时充当处理程序。
updateForm($(ListenerFieldId[“roomCountID”]).value);
}
var calculateLbsOfSoap=函数(rmCnt){//计算soap的重量并返回数量。
返回支票计数(rmCnt)?0:((soapOuncesRecoverable*lbConversion)*(rmCnt*OccupencyRate)*收集持续时间);
}
var calculatelsofblacks=函数(rmCnt){//计算瓶装便利设施的重量并返回数量。
返回支票计数(rmCnt)?0:((瓶装盎司可回收*磅换算)*(rmCnt*职业年期)*收集持续时间);
}
var calculateBarsOfSoap=函数(rmCnt){//计算房间计数不为0时分布的条数。
返回校验计数(rmCnt)?0:((rmCnt)*16)/3);
}
var calculateBottles=function(rmCnt){//如果房间计数不是0,则计算分发了多少瓶。
返回检查计数(rmCnt)?0:((计算瓶数(rmCnt)*16)/估计瓶数)*(2/3));
}
var calculatePrice=函数(rmCnt){
返回支票计数(rmCnt)?0:(rmCnt*比率);
}
var checkCount=函数(count){//如果计数为0或小于0,则该数字无效,因此只需返回0即可防止出现奇数结果。
返回(计数<0 | |计数==0)?真:假;
}
var initializerAltimeCalctoForm=函数(){
如果(窗口附件){
$(ListenerFieldId[“roomCountID]”)。attachEvent(“onkeydown”,ListenForNumber,false);
$(ListenerFieldId[“roomCountID]”)。attachEvent(“onkeyup”,ListenForNumber,false);
$(ListenerFieldId[“roomCountID]”)。附件(“onkeypress”,ListenForNumber,false);
$(ListenerFieldId[“roomCountID]”)。附件(“onchange”,ListenForNumber,false);
}否则{
//但如果不是,即…:-D
$(ListenerFieldId[“roomCountID]”)。addEventListener(“键控”,ListenForNumber,false);
$(ListenerFieldId[“roomCountID]”)。addEventListener(“keyup”,ListenForNumber,false);
$(ListenerFieldId[“roomCountID]”)。addEventListener(“按键”,ListenForNumber,false);
$(ListenerFieldId[“roomCountID]”)。addEventListener(“更改”,ListenForNumber,false);
}
}
window.onload=函数(){
初始化eraltimecalctoform();
}

如果只想将变量
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?

您能更好地解释这些变量的含义以及您想要什么吗?什么是支票计数?价格是多少?什么是rmCnt?您没有提出任何问题,因此我们不清楚如何提供帮助。请编辑你的问题,问它,告诉我们你在看什么,为什么它是错的。对不起,我已经更新了这个问题实际上是一个问题。你应该真正考虑使用jQuery,而不是手工做所有的IE特定的东西。节省了您大量的时间和精力。因此,在我的脚本中,我应该将其放置在哪里,myvar应该更改为什么?@JeremyChambers,您希望的值至少为60?我相信是checkCount(rmCnt)?0用于确定房间计数不是0。那么这将是一个不合逻辑的公式,并抛出一个错误。谢谢你,这工作完美。刚刚用了你的第一个例子。我对javascript的了解已经足够危险了。真的吗