我有两个用户输入数据的字段,RoomCount 和contractedNights。我不知道我哪里出错了。我已经添加了我的听众,并尝试了一系列不同的方法来让这些字段相乘,并在 calculatePrice 函数中显示最小因子为 60 (math.max60) 的结果。有人可以帮我看看/了解我做错了什么吗?谢谢你。
var listenerFieldIDs = {
"roomCountID": "item4_text_1",
"contractedNightsID": "item8_text_1"
};
var impactFields = ["item12_text_1", "item1_text_1", "item16_text_1", "item18_text_1", "item20_text_1"];
var estimatedBottleSize = 1.5,
occupancyRate = (60 / 100),
collectionDuration = 365,
soapOuncesRecoverable = 0.63,
bottleOuncesRecoverable = 0.47,
lbConversion = 0.0626;
var $ = function (id) {
return document.getElementById(id);
}
var updateFormField = function (id, amount) {
$(id).value = amount;
}
var updateForm = function (roomCount, contractedNights) {
updateFormField(impactFields[0], addCommas(calculateLbsOfSoap(roomCount).toFixed(2)));
updateFormField(impactFields[1], addCommas(calculateLbsOfBottles(roomCount).toFixed(2)));
updateFormField(impactFields[2], addCommas(calculateBarsOfSoap(roomCount).toFixed(0)));
updateFormField(impactFields[3], addCommas(calculateBottles(roomCount).toFixed(0)));
updateFormField(impactFields[4], ("$" + addCommas(calculatePrice(checkCount(roomCount), contractedNights).toFixed(2))));
}
var listenForNumbers = function (event) {
updateForm($(listenerFieldIDs["roomCountID"]).value, $(listenerFieldIDs["contractedNightsID"]).value);
}
var calculateLbsOfSoap = function (rmCnt) {
return checkCount(rmCnt) ? 0 : ((soapOuncesRecoverable * lbConversion) * (rmCnt * occupancyRate) * collectionDuration);
}
var calculateLbsOfBottles = function (rmCnt) {
return checkCount(rmCnt) ? 0 : ((bottleOuncesRecoverable * lbConversion) * (rmCnt * occupancyRate) * collectionDuration);
}
var calculateBarsOfSoap = function (rmCnt) {
return checkCount(rmCnt) ? 0 : ((calculateLbsOfSoap(rmCnt) * 16) / 3);
}
var calculateBottles = function (rmCnt) {
return checkCount(rmCnt) ? 0 : (((calculateLbsOfBottles(rmCnt) * 16) / estimatedBottleSize) * (2 / 3));
}
var calculatePrice = function (rmCnt, contractedNights) {
return checkCount(rmCnt, contractedNights) ? 0 : (calculatePrice(rmCnt) * (contractedNights));
}
var checkCount = function (count) {
return (count < 0 || count == 0) ? true : false;
}
var addCommas = function (num) {
var result = "",
numString = "",
numOfDigits = 0;
numString = num.toString();
numOfDigits = numString.indexOf(".");
if (numOfDigits == -1) {
numOfDigits = numString.length;
}
if (numOfDigits < 4) {
result = num;
} else {
var extraDigits = numOfDigits % 3;
var count = extraDigits;
if (count != 0) {
result = numString.substring(0, extraDigits) + ",";
}
while (count < numOfDigits) {
/
if(numOfDigits - count == 3){
break;
}
result = result + numString.substring(count,count + 3) + ",";
count = count + 3; }
result = result + numString.substring(count,count + 3) + numString.substring(numOfDigits);
}
return result;
}
var stripCommas = function(formattedNumber){
var strippedFormat = "";
var pulledNumbers = formattedNumber.match(/ [0 - 9\.] / );
for (var i = 0; i < pulledNumbers.length; i++) {
strippedFormat = strippedFormat + pulledNumbers[i];
}
return parseFloat(strippedFormat);
}
var initializeRealTimeCalcToForm = function () { //Used to load the needed functionality to the form.
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);
$(listenerFieldIDs["contractedNightsID"]).attachEvent("onkeydown", listenForNumbers, false);
$(listenerFieldIDs["contractedNightsID"]).attachEvent("onkeyup", listenForNumbers, false);
$(listenerFieldIDs["contractedNightsID"]).attachEvent("onkeypress", listenForNumbers, false);
$(listenerFieldIDs["contractedNightsID"]).attachEvent("onchange", listenForNumbers, false);
} else {
$(listenerFieldIDs["roomCountID"]).addEventListener("keydown", listenForNumbers, false);
$(listenerFieldIDs["roomCountID"]).addEventListener("keyup", listenForNumbers, false);
$(listenerFieldIDs["roomCountID"]).addEventListener("keypress", listenForNumbers, false);
$(listenerFieldIDs["roomCountID"]).addEventListener("change", listenForNumbers, false);
$(listenerFieldIDs["contractedNightsID"]).addEventListener("keydown", listenForNumbers, false);
$(listenerFieldIDs["contractedNightsID"]).addEventListener("keyup", listenForNumbers, false);
$(listenerFieldIDs["contractedNightsID"]).addEventListener("keypress", listenForNumbers, false);
$(listenerFieldIDs["contractedNightsID"]).addEventListener("change", listenForNumbers, false);
}
}
window.onload = function () { //This will kick start the process to install the functionality onto your form.
initializeRealTimeCalcToForm();
}