使用JavaScript循环遍历

    <script type="text/javascript">
        $(function () {
            var allComNum = [];//定义一个变量数组
            var result = 0;//定义一个变量
            $(".com_num1").each(function (i, n) { //循环
                allComNum.push(parseInt($(n).val()));
            });
            for (var i = 0; i < allComNum.length; i++) {
                result += allComNum[i];
            }
            $(".ShopCartNum").html(result);
        });
    </script>

push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度。

each() 方法规定为每个匹配元素规定运行的函数。

parseInt() 函数可解析一个字符串,并返回一个整数。

原文地址:https://www.cnblogs.com/seeyougirl/p/8267229.html