jquery sum of multiple input fields if same class in one input

前端 未结 8 1427
迷失自我
迷失自我 2020-12-14 02:49

Hello I need to sum the values of same class input in one input with class name total.




        
相关标签:
8条回答
  • 2020-12-14 03:29

    We can use following own function

     (function( $ ){
       $.fn.sum=function () {
        var sum=0;
            $(this).each(function(index, element){
                sum += parseFloat($(element).val());
            });
        return sum;
        }; 
    })( jQuery );
    //Call $('.abcd').sum();
    

    http://www.gleegrid.com/code-snippet/javascript/jquery-sum-input-values-by-class/?filter=bygroup&group=JQuery

    0 讨论(0)
  • 2020-12-14 03:33

    You pretty much had it, just needed to adjust your JQuery a little bit for the appropriate selectors

    updated fiddle : http://jsfiddle.net/5gsBV/7/

    $(document).on("change", ".qty1", function() {
        var sum = 0;
        $(".qty1").each(function(){
            sum += +$(this).val();
        });
        $(".total").val(sum);
    });
    
    0 讨论(0)
提交回复
热议问题