Javascript - 添加 var 的问题

Javascript - issue with var addition

任何人都可以看看为什么 JS 我连接值而不是添加它们吗? https://codepen.io/mateusz-baran/pen/XWpGdvj

$(document).ready( function () {


// VARIABLES
// ----------------------------------------------------------

var amount, percent, result;
var calculator = $('.calculator');
  var simAmount = calculator.find('.calculator__numsims');
    var pricePerUse = calculator.find('.calculator__peruse');

    var daysOpen = calculator.find('.calculator__daysopen');
    var occupancy = calculator.find('.calculator__occupancy');
    
    var monthlyRev = calculator.find('.calculator__monthlyrev');
    var yearlyRev = calculator.find('.calculator__yearlyrev');
    var roiEta = calculator.find('.calculator__roieta');
var calculatorResult = calculator.find('.calculator__result');
var txtPerUse = calculator.find('.peruse');
  var txtSimNum = calculator.find('.simnum');
  var txtDaysOpen = calculator.find('.daysopen');
  var txtOccupancy = calculator.find('.occupancy');

      var txtMonthlyRev = calculator.find('.calculator__monthlyrev');
      var txtYearlyRev = calculator.find('.calculator__yearlyrev');
      var txtRoiETA = calculator.find('.calculator__roieta');



// INIT BILL
// ----------------------------------------------------------

$(window).on('DOMContentLoaded', function () {

        calcAmount();

});


// RANGE FUNCTION
// ----------------------------------------------------------

calculator.on('input', function () {
            calcAmount();
});

function calcAmount()
{
        txtDaysOpen.text(daysOpen.val());
        txtOccupancy.text(occupancy.val());
        
        txtSimNum.text(simAmount.val());
        txtPerUse.text(pricePerUse.val());

        calculatorResult.text(addCommas(((simAmount.val() + occupancy.val()))));
        
        txtMonthlyRev.text(addCommas((((((occupancy.val() / 100) * pricePerUse.val()) * simAmount.val() ) ) * daysOpen.val()) * 4));
        
        txtYearlyRev.text(addCommas(((((((occupancy.val() / 100) * pricePerUse.val()) * simAmount.val() )) * daysOpen.val()) * 4) * 12));
        
        txtRoiETA.text(parseInt((calculatorResult.text().replace(',','') / txtMonthlyRev.text().replace(',','')) + " months"));
    
    
};

function addCommas(nStr) {
    var tst = Math.round(nStr);
  var num = (tst + "").replace(/\b(\d+)((\.\d+)*)\b/g,                  function(a, b, c) {
        return (b.charAt(0) > 0 && !(c || ".").lastIndexOf(".") ? b.replace(/(\d)(?=(\d{3})+$)/g, ",") : b) + c;

这是 JS 部分的第 55 行。

加载默认数据后 - 我希望看到的结果为:110,000,但我得到的是 10,000,010,000 - 所以数字被连接起来而不是相加

有人可以帮忙吗?

尝试使用 parseInt()parseFloat() 将字符串变量更改为整数,例如 calculatorResult.text(addCommas(((parseInt(simAmount.val()) + parseInt(occupancy.val())))));

在 JS 中,+ 运算符除了添加数字变量外,还连接字符串类型的变量。

这就是罪魁祸首。将 parseInt 添加到该值。这会将字符串转换为整数

  calculatorResult.text(addCommas(((parseInt(simAmount.val()) + parseInt(occupancy.val())))))

对代码中的任何其他添加执行此操作