-2

我不断得到 $NaN 而不是数字。(代码从 . 开始line 155。)如何获得数字输出而不是$NaN. 是否有更好的方法来调试它以确定返回的确切内容?

function daysInMonth(y, m) {
  var t = new Date(y, m, 0);
  return t.getDate();
}
function daysLeftInQuater(d) {
  d = d || new Date(); // If no date supplied, use today
  var t = new Date(d); // Don't mess up supplied date if there was one
  var daysLeft = 2 - (t.getMonth() % 3);

  do {
    daysLeft += daysInMonth(t.getFullYear(), t.getMonth() + 1) - t.getDate();
    t.setMonth(t.getMonth() + 1);
    t.setDate(1);
  } while (t.getMonth() % 3)

  return daysLeft;      
}
function getQuarter(d) {
  d = d || new Date();
  var m = Math.floor(d.getMonth()/3) + 2;
  return m > 4? m - 5 : m;
}
function hoa_letsgo(){
var amt_due = $("#user_ad").val();
var qma_val = $("#qma_select").val();
if (qma_val == 1){//Q
var cur_year = $("#year").val(); var cur_day = $("#day").val();
var cur_month = $("#month").val(); var now = new Date(cur_month, cur_day, cur_year);
var days_left = daysLeftInQuater(cur_month);
var ind_amt = amt_due / 4;
var day_amt = amt_due / 360;
var quarter = getQuarter(now);
var am_cred = ind_amt * quarter;
var final_cred = am_cred + (day_amt * days_left);
final_cred = final_cred - amt_due;
final_cred = final_cred.toFixed(2)
$("#credit").html("$" + addCommas(final_cred));
}
4

1 回答 1

1

从文本框中拉出一个值会给你一个字符串。您不能使用字符串执行某些算术函数,因为运算符被重载以执行不同的操作。例如,+如果任一操作数是字符串,加法运算符将执行字符串连接。

用or包裹你的所有.val()电话,它应该可以解决你的问题。parseInt(..., 10);parseFloat(..., 10);

于 2012-08-16T15:30:06.403 回答