gpt4 book ai didi

javascript - 当所有变量和输入都是数字时,为什么我会得到 NaN?

转载 作者:行者123 更新时间:2023-11-29 17:16:18 25 4
gpt4 key购买 nike

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

最佳答案

在你的代码中你有:

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;

关于javascript - 当所有变量和输入都是数字时,为什么我会得到 NaN?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17449796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com