javascript - 总计和总数量未显示所有项目的添加

标签 javascript jquery arrays

脚本:

 function subTotal3(param)
  {

        var product_quantity = 0;
        var product_price = 0;
        var gst_amount = 0;

        var sub_total = 0;
        var total_qty = 0; 
        var grand_total = 0;
        var splitty;
        var unit_price;

       $('input[id=qty_'+param+']').each( function( k, v ) {

           /*starts here*/
           product_quantity = parseInt ( $(this).val() ) ? parseInt ( $(this).val() ) : 0;
           product_price = $(this).parent().prev().text()? $(this).parent().prev().text():0;
          /*ends here*/

           splitty = product_price.split('RM');
           unit_price = splitty[1];
           sub_total = parseFloat (unit_price * product_quantity);

           $(this).parent().next().val(sub_total);
           $(this).parent().next().text(sub_total);

            total_qty += product_quantity;

            grand_total += sub_total;

        });

        alert(total_qty);
        $('.qty_1').text(total_qty);
        $('.total').text(grand_total);
  }

这是输出(注意总计下的值):

enter image description here

“总计”下的值显示当前所选项目的总数量和小计。据说,它应该显示这两项的总计。

我猜想,声明 var total_qty = 0;var grand_total = 0; 导致它,因此我声明它们而不分配值 0 然后它显示 AmountNaN。我尝试将 total_qty += Product_quantity; 放在 $.each 循环之外,但出现同样的问题。请问如何添加总数量和总金额?

最佳答案

尝试在输入中添加类,例如class="qty"。然后将 $('input[id=qty_'+param+']') 替换为 $('.qty')

关于javascript - 总计和总数量未显示所有项目的添加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34792985/

相关文章:

javascript - 为 Phonegap 应用程序创建联系表单

javascript - 异步/等待操作发生在预期的顺序之外

javascript - 如何在 JavaScript 中将变量传递给新对象?

javascript - 使用 jquery 在 <sup>®</sup> 中包装 HTML 文档中的所有 ® 字符

javascript - 滚动时堆叠 div

php - jQuery ajax 调用后将 SQL 数据返回 JavaScript

javascript - this.setState() 不使用传递的数组更新状态

具有属性的 Javascript 数组?

c++ - 为什么这个 for 循环条件不起作用?

arrays - 如何从 jQuery 对象数组创建 jQuery 对象?