jQuery-在特定类的范围内添加所有数字 [英] Jquery - Adding all numbers inside span of a particular class

查看:80
本文介绍了jQuery-在特定类的范围内添加所有数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个抓了我的头,我可以在jQuery中完成div的常规内容+另一个div的内容,但是我不确定如何将页面上特定跨度的所有实例的内容加在一起.基本上,我可以创建一个使用jquery和ajax将信息提取到其中的页面,它提取选定月份的发票并在每一行中显示它们.我想做的是将行与"grandtotal"类一起添加,因此我可以显示该月所有发票的月度发票总额,但是我不确定如何添加"grandtotal" +"grandtotal"等当我不知道每次将生成多少个跨度实例时,我不知何故需要从页面上当前"grandtotal"的每个实例中获取数字,然后将其显示在div"monthlytotal"中.这是从ajax中提取每个发票行的代码.

im scratching my head with this one, I can do the usual contents of div + contents of another div addition in jquery, but im not sure how I go about adding together the contents of all instances of a particular span on a page. Basically I can a page that pulls info into it using jquery and ajax, it pulls in invoices for the month selected and displays them in each row. What I want to do is add off the the rows together with the class 'grandtotal', so I can display the monthly invoice total for all the invoices for that month, but im not sure how to add 'grandtotal' + 'grandtotal' etc when I dont know how many instances of that span are going to be generated each time, I somehow need to take the number from each instance of 'grandtotal' currently on the page and then display it in a div 'monthlytotal' . This is the code that pulls each invoice row from the ajax.

 $.getJSON('select.php', {monthyear: $(this).val()}, function(data){
                var invoicerow = '';
                for (var x = 0; x < data.length; x++) {
                    if (data[x]['datepaid'] == '0000-00-00'){datepaid = '<span style="color:red;">Not Paid</span>'}else{datepaid = data[x]['datepaid']};
                    invoicerow += '<tr><td>' + data[x]['invoiceID'] + '</td><td>' + data[x]['customerID'] + '</td><td>' + data[x]['date'] + '</td><td>£' + '<span class="grandtotal">' + data[x]['grandtotal'] + '</span>' + '</td><td>' + '<a target="_NEW" href="../salesmanager/viewinvoice.php?&salesID=' + data[x]['salesID'] + '">View Invoice</a></td><td>' + datepaid + '</td></tr>';
                }
                $('#summarycontent').html(invoicerow);
              $("select").select2();

编辑>>>>>

好的,到目前为止,我可以在这里工作,但是问题是它乘以上个月....因此,当页面上加载选择框时,它说"Total:"(Total :),第一次按下并选择select Januaury时它说0英镑,然后选择2月,您将得到1860英镑,但问题是1月的总和而不是2月份的2月份的总和是0英镑,因此结果似乎一直都落后于1.

Ok so I am here so far, this works, but the problem is its multiplying the previous month.....so when the page loads with the select box it says Total: , when its first pressed and say select Januaury it says £0 , then select feb and you get £1860 , but the thing is thats Januarys total and not Feb's , febs total is £0 , so its as though the result is one behind all the time.

<script>
                    $(document).ready(function(){
    $('#selectmonth').on('change', function (){
                        var sum = 0;
$('.grandtotal').each(function(){
 sum += parseFloat($(this).text());
});
$('#total_price').text('£' + sum);
        });   
            });
</script>

推荐答案

好的,这似乎可行:

<script>
                    $(document).ready(function(){
    $('#invoices').bind('DOMNodeInserted DOMNodeRemoved', function() {
                        var sum = 0;
$('.grandtotal').each(function(){
var tal = $(this).text();
if(isNaN(tal)) {
sum += 0;
}else{
sum += parseFloat($(this).text());};
});
 $('#total_price').text('£' + sum);
        });   
            });
</script>

这篇关于jQuery-在特定类的范围内添加所有数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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