jQuery calculate sum of values in all text fields

前端 未结 9 1250
渐次进展
渐次进展 2020-11-29 19:52

I have an order form with about 30 text fields that contain numerical values. I\'d like to calculate the sum of all those values on blur.

I know how to select all te

9条回答
  •  日久生厌
    2020-11-29 20:11

    Similarly along the lines of these answers written as a plugin:

    $.fn.sum = function () {
        var sum = 0;
        this.each(function () {
            sum += 1*($(this).val());
        });
        return sum;
    };
    

    For the record 1 * x is faster than Number(x) in Chrome

提交回复
热议问题