本文介绍了jQuery计算所有文本字段中的值之和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大约30个包含数字值的文本字段的订单.我想计算模糊时所有这些值的总和.

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 text fields but not how to loop through them and add up all their values?

$(document).ready(function(){
   $(".price").blur(function() {
    //loop and add up every value from $(".price").val()
   })
});

推荐答案

$('.price').blur(function () {
    var sum = 0;
    $('.price').each(function() {
        sum += Number($(this).val());
    });

    // here, you have your sum
});​​​​​​​​​

这篇关于jQuery计算所有文本字段中的值之和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 17:33