English 中文(简体)
如何在一张桌子上使用 j子添加价值?
原标题:How to Add values in a table together using jQuery?

我有一个带有数字价值的超文本表格。 我需要做的是,利用 j子把所有这些价值观加在一起。

任何想法?


例表:

http://pastie.org/998759

最佳回答

页: 1

var sum = 0;

$( table td ).each(function() {
    sum += parseFloat($(this).text());
});
问题回答

我会给你一些假装的密码,说明我是如何做的(主要因为我无法做全事)。

  1. Create an object/array/variables to store the totals.
  2. jQuery select the table
  3. Use .each() to loop through each tr.
  4. Loop each td and add the value to the relevant total.
  5. Once all of the tr have been looped through, do whatever you want :)

希望有助于你们:

EDIT:我写的是,如果你想要把每栏一整起来,而不是把所有囚室一整一整。

这一点已经得到答复。 因此,就象 exercise:

// Give all arrays the power of summation    
Array.prototype.sum = function(){
    for( var i=0,sum=0;i<this.length;sum+=this[i++] );
    return sum;
}

// Use map for kicks
$(document).ready( function(){
    var result = $( table#mytable tr td ).map( function(){
        return parseFloat( $(this).text() );
    }).get().sum();
});




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签