2

我知道我还没有设置验证,但我不知道为什么我在 2/5 结果字段中得到 NaN。任何帮助将不胜感激!

产生 NaN 的字段是 interestPay 和 gainLoss。我知道 gainLoss 与 interestPay 有关,所以如果我能修复那个,它可能会修复两者。

在计算之后,我尝试在monthlyBalance 上使用parseFloat 和parseInt,但这不起作用。

有什么建议吗?

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Bankrate Payment Calculator Test | Todd Adel</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Bootstrap -->
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="css/bootstrap-responsive.min.css" rel="stylesheet" media="screen">
<link href="css/style.css" rel="stylesheet" media="screen">
<script type="text/javascript">

function calculateThis(form) {
    //Get a reference to the form id="calculator"
    var theForm = document.forms["calculator"];

    var balance = parseFloat(document.getElementById('balance').value);
    var charge  = parseFloat(document.getElementById('charge').value);
    var spending1 = parseFloat(document.getElementById('spending1').value);
    var spending2 = parseFloat(document.getElementById('spending2').value);
    var spending3 = parseFloat(document.getElementById('spending3').value);
    var cashBack1 = (parseFloat(document.getElementById('cashBack1').value)) / 100;
    var cashBack2 = (parseFloat(document.getElementById('cashBack2').value)) / 100;
    var cashBack3 = (parseFloat(document.getElementById('cashBack3').value)) / 100;
    var everythingElse = parseFloat(document.getElementById('everythingElse').value);
    var cashBackEE = (parseFloat(document.getElementById('cashBackEE').value)) / 100;
    var payOff = parseFloat(document.getElementById('payOff').value);
    var interestRate = (parseFloat(document.getElementById('interestRate').value)) / 100;

    // Calculate monthlyBalance
    var monthlyBalance = document.getElementById('monthlyBalance');
    monthlyBalance.innerHTML  = "$" + (balance + charge - payOff);

    // Calculate interestPay
    var interestPay = document.getElementById('interestPay');
    interestPay.innerHTML = "$" + monthlyBalance * interestRate / 12;

    // Calculate cashBackEarned
    var cashBackEarned = document.getElementById('cashBackEarned');
    cashBackEarned.innerHTML = "$" + ((spending1 * cashBack1) + (spending2 * cashBack2) + (spending3 * cashBack3) + (everythingElse * cashBackEE));

    // Calculate gainLoss
    var gainLoss = document.getElementById('gainLoss');
    gainLoss.innerHTML = "$" + (cashBackEarned - interestPay);

    // Calculate earnedRewards
    var divobj = document.getElementById('earnedRewards');
    if (cashBackEarned >= 0) {
        var earnedRewards = true;
        divobj.innerHTML = "Yes";
    } else {
        earnedRewards = false;
        divobj.innerHTML = "No";
    }

}    


</script>
</head>
<body>
<form id="calculator" class="form-inline">
<div id="container">

   <div id="inner" class="row">   

    <div class="row header"><h1>Are you paying for cash-back rewards?</h1></div>
    <div class="row subhead"><h2>Fill out the form below to find out now</h2></div>
    <div class="row data">
        <div class="span10 question">What was your balance left over from last month?:</div>
        <div class="span2 input"><span>$</span><input class="input-mini" name="balance" id="balance" type="text" /></div>
    </div>
    <div class="row data">
        <div class="span10 question">How much did you charge this month?</div>
        <div class="span2 input"><span>$</span><input class="input-mini" name="charge" id="charge" type="text" /></div>
    </div>
    <div class="row data">
        <div class="span10 question">Choose a spending category
          <select name="spending_category" size="1" class="input-med">
            <option value="1">Spending Category 1</option>
            <option value="2">Spending Category 2</option>
            <option value="3">Spending Category 3</option>

          </select>
      </div>
        <div class="span2 input"><span>$</span><input class="input-mini" name="spending1" id="spending1" type="text" /></div>
    </div>
    <div class="row data">
        <div class="span10 question">What was your % Cash Back?</div>
        <div class="span2 input"><span>%</span><input class="input-mini" name="cashBack1" id="cashBack1" type="text" /></div>
    </div>
    <div class="row data">
        <div class="span10 question">Choose a spending category
          <select name="spending_category" size="1" class="input-med">
            <option value="1">Spending Category 1</option>
            <option value="2">Spending Category 2</option>
            <option value="3">Spending Category 3</option>

          </select>
      </div>
        <div class="span2 input"><span>$</span><input class="input-mini" name="spending2" id="spending2" type="text" /></div>
    </div>
    <div class="row data">
        <div class="span10 question">What was your % Cash Back?</div>
        <div class="span2 input"><span>%</span><input class="input-mini" name="cashBack2" id="cashBack2" type="text" /></div>
    </div>
    <div class="row data">
        <div class="span10 question">Choose a spending category
          <select name="spending_category" size="1" class="input-med">
            <option value="1">Spending Category 1</option>
            <option value="2">Spending Category 2</option>
            <option value="3">Spending Category 3</option>

          </select>
      </div>
        <div class="span2 input"><span>$</span><input class="input-mini" name="spending3" id="spending3" type="text" /></div>
    </div>
    <div class="row data">
        <div class="span10 question">What was your % Cash Back?</div>
        <div class="span2 input"><span>%</span><input class="input-mini" name="cashBack3" id="cashBack3" type="text" /></div>
    </div>
    <div class="row data span12"><a href="#" class="btn btn-primary" style="float:right">Add Category</a></div>
    <div class="row data">
        <div class="span10 question">What did you spend on everything else?</div>
        <div class="span2 input"><span>$</span><input class="input-mini" name="everythingElse" id="everythingElse" type="text" /></div>
    </div>
    <div class="row data">
        <div class="span10 question">What was your % Cash Back?</div>
        <div class="span2 input"><span>%</span><input class="input-mini" name="cashBackEE" id="cashBackEE" type="text" /></div>
    </div>
    <div class="row data">
        <div class="span10 question">How much did you pay off?</div>
        <div class="span2 input"><span>$</span><input class="input-mini" name="payOff" id="payOff" type="text" /></div>
    </div>
    <div class="row data">
        <div class="span10 question">Interest rate on credit card</div>
        <div class="span2 input"><span>%</span><input class="input-mini" name="interestRate" id="interestRate" type="text" /></div>
    </div>
    <div class="row data span12"><a href="#" class="btn btn-primary" style="float:right" onclick="calculateThis()">Calculate</a></div>

    <div class="row span12"><h2>Results</h2></div>
    <div id="results">
      <div class="row data blue">
          <div class="span10 question">Monthly Balance</div>
        <div id="monthlyBalance" class="span2 input"><span>$</span></div>
      </div>
      <div class="row data white">
          <div class="span10 question">How much will you pay in interest?</div>
          <div id="interestPay"  class="span2 input"></div>
      </div>
      <div class="row data blue">
         <div class="span10 question">Cash back earned this month</div>
          <div id="cashBackEarned"  class="span2 input"></div>
      </div>
      <div class="row data white">
          <div class="span10 question">How much $ did you gain / lose?</div>
          <div id="gainLoss"  class="span2 input"></div>
      </div>
      <div class="row data blue">
          <div class="span10 question">Did you pay for your rewards?</div>
          <div id="earnedRewards"  class="span2 input"></div>
      </div>

    </div> <!-- End #results -->

  </div> <!-- End #inner -->

</div> <!-- End #container -->
</form>
<script src="http://code.jquery.com/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
</html>
4

4 回答 4

3

在您的代码中,您有:

var monthlyBalance = document.getElementById('monthlyBalance');

这将检索 ID 为“monthlyBalance”的 DOM 元素。

后来你有:

interestPay.innerHTML = "$" + monthlyBalance * interestRate / 12;

这意味着您正在尝试将 DOM 元素(不是数字)与 interestRate / 12 相乘。

尝试将您的代码更改为以下内容:

// Calculate monthlyBalance
var monthlyBalance = balance + charge - payOff;
var monthlyBalanceDomObject = document.getElementById('monthlyBalance');
monthlyBalanceDomObject.innerHTML  = "$" + monthlyBalance;
于 2013-07-03T13:57:34.527 回答
1

好像你错过了.value这个功能

document.getElementById('monthlyBalance')

应该

document.getElementById('monthlyBalance').value;
于 2013-07-03T13:52:34.303 回答
1

首先,您获得一个 DOM 元素:var monthlyBalance = document.getElementById(..);,然后您尝试在计算中使用该元素:interestPay.innerHTML = "$" + monthlyBalance * interestRate / 12;

您需要为#monthlyBalance元素及其计算值创建单独的变量。

cashBackEarnedinterestPay你计算#gainLoss.

于 2013-07-03T13:58:14.373 回答
0
interestPay.innerHTML = "$" + monthlyBalance * interestRate / 12;

此时monthlyBalace是一个html元素,可以试试这个:

interestPay.innerHTML = "$" + (balance + charge - payOff) * interestRate / 12;

那么你在 cashBackEarned 和 interestPay 上犯了同样的错误。您希望它们是数字,但它们是 html 元素。

于 2013-07-03T14:05:52.717 回答