简体   繁体   English

为什么我获得NaN值?

[英]why am I getting NaN value?

I'm trying to figure out why valInt is returning NaN and how I can fix it so that it stores deposit value. 我试图弄清楚valInt为什么返回NaN以及如何解决它以便存储存款价值。

*Edit: Made some changes since posting. *编辑:自发布以来进行了一些更改。 I'm still getting NaN for the balance. 我仍然需要NaN来支付余额。

//JS // JS

`function depositMoney () {
//Select the balance amount
let acctBal = $('.check-balance')
let chkBal = acctBal
let chkBalVal = chkBal.html()
console.log(chkBalVal)

//Needs to accept input.
let input = $('.input')
let depositVal = parseInt(input.val())
console.log('Deposit amount: ' + depositVal)

//Collect info from balance.
let valInt = parseInt(chkBalVal)
console.log('Current balance is: ' + valInt)

//Displaying the checking amount.
let sumTot = (depositVal + valInt)
console.log('Total here:' + sumTot)
acctBal.html('$' + (depositVal + valInt))
}`

// HTML // HTML

`<div id="checking" class="account">
  <h2>Checking</h2>
  <div class="check-balance">$0</div>
  <input class="input" type="text" placeholder="enter an amount" />
  <input class="deposit" type="button" value="Deposit" />
  <input class="withdraw" type="button" value="Withdraw" />
  </div>`
let valInt = parseInt(chkBal)

This code is trying to convert chkBal to number, where chkBal is exactly like acctBal - a jQuery object. 这段代码试图将chkBal转换为数字,其中chkBal就像acctBal一样-一个jQuery对象。

You have to take the value that stored in jQuery DOM object with the command .val() , and then convert the result to a number by modifying the line beginning with let chkBal = like so: 您必须使用.val()命令获取存储在jQuery DOM对象中的值,然后通过修改以let chkBal =开头的行将结果转换为数字,如下所示:

let chkBal = acctBal.val();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM