jQuery表行总和总计 [英] jquery table rows line totals and grand total

查看:113
本文介绍了jQuery表行总和总计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为计算机维修技术人员编写一个小插件,我现在正在尝试对发票部分进行编码,但运气不佳!

我已经从数据库中填充了字段,但是我试图使用jquery更新行总数和发票总数.

这里有我要做什么的快速图片!

我正在尝试获取它来更新页面加载量以及单位成本和数量文本字段中的值更改时的总数.

$(document).ready(function() {
    $("#invoiceitems tbody tr").each(function() {
        $(this).change(function() {
            updateTotal();
        });
    });
});

function updateTotal() {

    //Calculate Subtotal for each item
    var quantity = $('.quantity').val();
    var price = $('.price').val();
    var subtotal = parseInt(quantity) * parseFloat(price);
    $('.subtotal').text(subtotal);
    //Calculate Grand Total
    var sum = 0;
    $('.linetotal').each(function() {
        sum += parseFloat($(this).text());
    });
    $('#grandTotal').html(parseFloat(sum));
}​

在这里找到了这段代码,但我确实没有任何线索,无法使其正常工作!

一些指导/帮助将不胜感激!

非常感谢 杰斯

解决方案

看起来您是jQuery的完整新手.不过,这是一个好的开始.

.price,.quantity和.lineTotal对应于他们正在谈论的列的类名. grandTotal是表中总计"的ID.

对于您尝试研究的案例,我已经提出了工作示例已更新 ..应该是一个好的开始.

但是请注意,我没有添加任何条件来检查用户输入是否有效...在推出时请尝试添加这些条件.让我知道您在解码时是否遇到任何困难.

I'm currently writing a small plugin for computer repair techs and I'm now trying to code the invoice section but not having much luck!

I've got the fields being populated from the database but I'm trying to use jquery to update the line totals and the total of the invoice.

Heres a quick image of what I'm trying to do!

I'm trying to get it to update the totals on pageload and when a value changes in the unit cost and quantity textfields.

$(document).ready(function() {
    $("#invoiceitems tbody tr").each(function() {
        $(this).change(function() {
            updateTotal();
        });
    });
});

function updateTotal() {

    //Calculate Subtotal for each item
    var quantity = $('.quantity').val();
    var price = $('.price').val();
    var subtotal = parseInt(quantity) * parseFloat(price);
    $('.subtotal').text(subtotal);
    //Calculate Grand Total
    var sum = 0;
    $('.linetotal').each(function() {
        sum += parseFloat($(this).text());
    });
    $('#grandTotal').html(parseFloat(sum));
}​

Found this code on here but I really don't have a clue and can't get it to work!

Some direction/help would be greatly appreciated!

Thanks very much Jase

解决方案

Looks like you are a complete novice to jQuery.. !! Good start though.. Not hard to pick it up..

Here .price , .quantity and .lineTotal corresponds to class names of the columns they are talking about.. grandTotal is the id of the Total in the table..

I have put up a Working Example Updated for the case you are trying to work into.. Should be a good start..

But do take care that I have not added any conditions to check if the user input is valid or not... Try adding those while rolling it out.. Let me know if you face any difficulty in decoding it.

这篇关于jQuery表行总和总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆