数字连接而不是添加

时间:2016-11-23 11:43:27

标签: javascript jquery

我的代码一切正常,直到我输入一个值来连接结果而不是添加。

function subtotal(){
    var a,b = 0;
    var row = $('#tbody tr').not(':first-child').length;
    for(i=0;i <= row;i++){
        a = parseFloat($('#i-total'+i).val());
        b += a;
    }
    $('#sub-total').val(parseFloat(Math.round(b * 100) / 100).toFixed(2));
    $('#sub-total').attr('value',parseFloat(Math.round(b * 100) / 100).toFixed(2));

    var dis = $('#disc-val').val();
    $('#disc-val').attr('value',dis);
    var tax = $('#tax').val();
    $('#tax').attr('value',tax);
    if(tax == '' || dis == ''){
      tax = 0;
      tax = parseFloat(tax);
      dis = 0;
      dis = parseFloat(dis);
     }
    var stat1 = $('#disc-val').attr('data-stat');   
    if(stat1 === 'on'){
        b = b-(b*dis/100);
        // console.log('disc%'+z);
    }
    else if(stat1 === 'off'){
        b = b - dis;
        // console.log('disc'+y);       
    }
    var stat2 = $('#tax').attr('data-stat');    
    if(stat2 === 'on'){
        b = (b*tax/100)+b;
    }
    else if(stat2 === 'off'){
        b = b + tax;
    }

    var ship = $('#shipping-val').val();    
    $('#shipping-val').attr('value',ship);
    if(ship == ''){
        ship = 0;
        ship = parseFloat(ship);
    }

    b = (ship + b);     

    $('#whole-total').attr('value', b);
}

3 个答案:

答案 0 :(得分:0)

如果变量ship不为空,则变量if(ship == ''){ ship = 0; ship = parseFloat(ship); } 是一个字符串。

这应该

if(ship == ''){
    ship = 0;
} else {
    ship = parseFloat(ship);
}

    app.all('/*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods", "GET, POST, PUT");
    next();
});

答案 1 :(得分:0)

尝试使用以下方式

&#13;
&#13;
 if(ship == ''){
        ship = 0;
        ship = parseFloat(ship);
    }

    b = parseFloat(ship) + parseFloat(b);   
&#13;
&#13;
&#13;

答案 2 :(得分:0)

这里的船舶价值处理不当:

var ship = $('#shipping-val').val();    
$('#shipping-val').attr('value',ship);
if(ship == ''){
    ship = 0;
    ship = parseFloat(ship);
}


if(ship == ''){
    ship = 0;
}
else{
  ship = parseFloat(ship);
}

或者它可以是

var ship = $('#shipping-val').val();    
$('#shipping-val').attr('value',ship);
if(ship == ''){
    ship = 0;
}

var ship = $('#shipping-val').val();    
$('#shipping-val').attr('value',ship);
if(ship == ''){
    ship = 0;

}


ship = parseFloat(ship); // out of conditional statement