简体   繁体   中英

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.

*Edit: Made some changes since posting. I'm still getting NaN for the balance.

//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

`<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.

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:

let chkBal = acctBal.val();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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