我知道我还没有设置验证,但我不知道为什么我在 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>