我知道我还没有设置验证,但我无法弄清楚为什么我会在2/5结果字段中获得NaN。任何帮助将不胜感激!
生成NaN的字段是interestPay和gainLoss。我知道gainLoss与interestPay有关,所以如果我可以修复那个,它可能会解决这两个问题。
我尝试在dailyBalance计算后使用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>
答案 0 :(得分: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;
答案 1 :(得分:1)
您似乎错过了.value
功能
document.getElementById('monthlyBalance')
应该是
document.getElementById('monthlyBalance').value;
答案 2 :(得分:1)
首先你得到一个DOM元素:var monthlyBalance = document.getElementById(..);
,然后你尝试在计算中使用该元素:interestPay.innerHTML = "$" + monthlyBalance * interestRate / 12;
。
您需要为#monthlyBalance
元素及其计算值创建单独的变量。
当您计算cashBackEarned
的值时,interestPay
和#gainLoss
代表相同的内容。
答案 3 :(得分:0)
interestPay.innerHTML = "$" + monthlyBalance * interestRate / 12;
此时,MonthlyBalace是一个html元素,可以试试这个:
interestPay.innerHTML = "$" + (balance + charge - payOff) * interestRate / 12;
然后你用cashBackEarned和interestPay犯了同样的错误。你希望它们是数字,但它们是html元素。