功能总和不能正常工作javascript

时间:2015-05-22 00:01:30

标签: javascript html

当我添加两个字段时,代码不会对它求和,而是添加我输入的内容,继承我的代码

function calculate_loan() {
  var amountBorrowed = document.form.amountBorrowed.value;
  var interest = document.form.interest.value / 100;
  var payments = document.form.durationOfPayment.value;
  var monthInterest = document.form.interestPerMonth.value;
  var totalInt = document.form.totalinterest.value;


 var monthly = amountBorrowed * interest;
 var totalinterest = monthly * payments;
 var totalAmount = amountBorrowed + totalinterest;


  if (!isNaN(monthly) &&
    (monthly != Number.POSITIVE_INFINITY) &&
    (monthly != Number.NEGATIVE_INFINITY)) {


    document.form.total.value = round(totalAmount);
    document.form.totalinterest.value =
      round(monthly * payments);
          document.form.interestPerMonth.value =
      round(monthly);
  }

  else {
    document.form.amountOfPayement.value = "";
    document.form.total.value = "";
    document.form.totalinterest.value = "";
  }
}

function round(x) {
  return Math.round(x * 100) / 100;
}

例如每月的利息是:720总利息:2160借款金额:12000,结果是120002160.我希望结果是14,160请帮助谢谢

1 个答案:

答案 0 :(得分:1)

您应该将值解析为数字

var amountBorrowed = Number(document.form.amountBorrowed.value);
var interest = Number(document.form.interest.value) / 100;
var payments = Number(document.form.durationOfPayment.value);
var monthInterest = Number(document.form.interestPerMonth.value);
var totalInt = Number(document.form.totalinterest.value);