为除零以外的autosum功能创建起点

时间:2016-03-29 17:04:03

标签: javascript jquery

我有一个自动求和单元,当前从0开始。我求和的单元格是输入框,但列表中也有静态框。我想要在自动求和功能中包含静态框,或者创建一个非0的替代起点。这是我当前的代码:

 $(document).ready(function(){

    //iterate through each textboxes and add keyup
    //handler to trigger sum event
    $('.budget').each(function() {

        $(this).keyup(function(){
            calculateSum();
        });
    });

});

function calculateSum() {

    var sum = 0;
    //iterate through each textboxes and add the values
    $('.budget').each(function() {

        //add only if the value is number
        if(!isNaN(this.value) && this.value.length!=0) {
            sum += parseFloat(this.value);
        }

    });
    //.toFixed() method will roundoff the final sum to 2 decimal places
    $("#total").html(sum.toFixed(0));
}

1 个答案:

答案 0 :(得分:0)

如何将初始值传递给函数:

function multiply(function(value){
return this * value;
});
}

DEMO