📌  相关文章
📜  如何在javascript代码示例中添加多个输入值

📅  最后修改于: 2022-03-11 15:01:19.542000             🧑  作者: Mango

代码示例1
$(document).ready(function(){

    //iterate through each textboxes and add keyup
    //handler to trigger sum event
    $(".input-number").each(function() {

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

});

function calculateSum() {

    var sum = 0;
    //iterate through each textboxes and add the values
    $(".input-number").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
    $("#sum").html(sum.toFixed(2));
}