0

ユーザーがデータを入力した2つのフィールド、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();
  }
4

1 に答える 1

0

変化する

updateFormField(impactFields[4], ("$" + addCommas(calculatePrice(checkCount(roomCount), contractedNights).toFixed(2))));

に:

updateFormField(impactFields[4], ("$" + addCommas(calculatePrice(roomCount, contractedNights).toFixed(2))));

var calculatePrice = function (rmCnt, contractedNights) {
  return checkCount(rmCnt, contractedNights) ? 0 : (calculatePrice(rmCnt) * (contractedNights));
}

var calculatePrice = function (rmCnt, contractedNights) {
  return checkCount(rmCnt) || checkCount(contractedNights) ? 0 : (rmCnt * contractedNights);
}
于 2012-08-28T14:49:50.570 に答える