Javascript on .each class getval

Pocket Money

thanks for looking at this question.

I have a issue as I got few text field

Their id is as followed

Row 1:

 height_1,weight_1,cost_1

Row 31:

   height_31,weight_31,cost_31

Row 61:

   height_61,weight_61,cost_61

Total will have 90 rows.. so the problem in the past is

Its work because I will use the javascript below to calculate the total of this 90 rows.

for(i=1;i<=90;i++)
{

$('.height').each(function () {
heightTotal += Number($(this).val());    
})

$("#height_row_A").html(heightTotal);

$('.weight').each(function () {
weightTotal += Number($(this).val());    
})

$("#weight_row_A").html(weightTotal);

//cost is just about the same so I leave it out
}

So the problem now is, in the past the 90 rows are calculated together and append to this span id height_row_total and weight_row_total

But now I need to calculate row 1 to 30, row 31 to 60 , row 61 to 90

Each have their on row span at the top of their row

which followed like this

Row 1

height_row_A,weight_row_A

Row 2

height_row_B,weight_row_B

Row 3

height_row_C,weight_row_C

How do I change my this

$('.height').each(function () {
heightTotal += Number($(this).val());    
})

$('.weight').each(function () {
weightTotal += Number($(this).val());    
})

to only get row 1 to 30, instead of every value with the id height and then to get row 31 to 60 and 61 to 90

My code segment is something like this example of a field. the 1 will change to 2 on the next row, there is total 30 rows x 3 column

<tr><td>1</td>

<td><input class="height" type="text" maxlength="6" style="width: 40px; FONT-WEIGHT: bold; FONT-SIZE: 13px;" name="height_1" id="height_1" tabindex="1" value=""></td>

<td><input class="weight" type="text" maxlength="6" style="width: 40px; FONT-WEIGHT: bold; FONT-SIZE: 13px;" name="weight_1" id="weight_1" tabindex="2" value=""></td>


For Row 2
   <tr><td>2</td>

    <td><input class="height" type="text" maxlength="6" style="width: 40px; FONT-WEIGHT: bold; FONT-SIZE: 13px;" name="height_2" id="height_2" tabindex="3" value=""></td>

    <td><input class="weight" type="text" maxlength="6" style="width: 40px; FONT-WEIGHT: bold; FONT-SIZE: 13px;" name="weight_2" id="weight_2" tabindex="4" value=""></td>

What I wanna to do is fetch the total height for row 1 - 30 , and display in a span id "total_height_for_col_1" and the same for weight

Then row 31-60 will be for col_2 and 61-90 will be for col_3

My JsFiddle , for the JsFiddle example, I got 4 boxes.

With the javascript, I wanted to able calculate the total for box 1 and 2, then box 3 and 4.

This example used only 4 boxes, in reality I got 90 boxes, and split by 30 per span id

JotaBe

You'd better use something more powerful like lodash.

This will allow you to easyly manipulate the arrays (remember that $('xxx') returns an array).

To get the ranges, use slice.

To get the value of each element as number use _.map()

To calculate the sum use _.reduce

Something like this:

_.reduce( 
     _.map( $('.height').slice(0,30), function(x){ return Number($(x).val());}), 
     function(accumulator, val) { return accumulator + val; })

(Obviously you need to slice by 30,60 and 60,90 to get the other results).

Explanation:

  • $('.height') returns an array of HTML elements
  • _.map applies the callback function to each element to get its value
  • _.reduce uses the callback to update the accumulator for each element (As there is no initial value specified for the accumulator, the first value will be use to initialize, and the following values to update it)

If you want to use plain javascript:

var ac=0;
var $elements = $('.height');
for(var i=0; i<30; i++) { 
  ac = ac + Number($($elements[i]).val());
};

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How to distinguish each class in intent?

분류에서Dev

Toggle class on each tr row when clicked

분류에서Dev

Find .offset of each element with X class

분류에서Dev

Array appending after each onclick and loop in javascript

분류에서Dev

Javascript Menu Setting Class to Active

분류에서Dev

Copy Javascript "Class" Prototype to an Object

분류에서Dev

Convert a Bibtex class object to a series of text strings formatted for each citation

분류에서Dev

jQuery append html element but with a different class name each time

분류에서Dev

GORM: automatically prefix each table name with the domain class namespace

분류에서Dev

For each list, count the total number of elements with matching class

분류에서Dev

create javascript class with classname including dot (my.class.name)?

분류에서Dev

$ .each (?

분류에서Dev

Javascript's scope "this" in Typescript's class

분류에서Dev

javascript find html <tr> with class and with nested <td>

분류에서Dev

Javascript inheriting properties and methods from a class?

분류에서Dev

how to pass init parameter to base class in javascript

분류에서Dev

Issues with calling internal functions in my javascript 'class'

분류에서Dev

Vanilla Javascript - add Class-name to an element

분류에서Dev

Javascript check if array item has class error

분류에서Dev

Javascript Class, var undefined in event listener

분류에서Dev

Javascript accessing class variables from window object

분류에서Dev

Why Flux can use "class" in Javascript?

분류에서Dev

JavaScript class & jQuery loop variable reference

분류에서Dev

Dynamic creation of element and class implementation in javascript

분류에서Dev

Javascript - how to define class factory as Extjs style?

분류에서Dev

set attribute for dynamically created class not working in javascript

분류에서Dev

JavaScript 개체의 [[Class]] 속성

분류에서Dev

JavaScript: Add prototype methods to inner class

분류에서Dev

Store Each Value of a AJAX Query Result to Javascript Variables

Related 관련 기사

  1. 1

    How to distinguish each class in intent?

  2. 2

    Toggle class on each tr row when clicked

  3. 3

    Find .offset of each element with X class

  4. 4

    Array appending after each onclick and loop in javascript

  5. 5

    Javascript Menu Setting Class to Active

  6. 6

    Copy Javascript "Class" Prototype to an Object

  7. 7

    Convert a Bibtex class object to a series of text strings formatted for each citation

  8. 8

    jQuery append html element but with a different class name each time

  9. 9

    GORM: automatically prefix each table name with the domain class namespace

  10. 10

    For each list, count the total number of elements with matching class

  11. 11

    create javascript class with classname including dot (my.class.name)?

  12. 12

    $ .each (?

  13. 13

    Javascript's scope "this" in Typescript's class

  14. 14

    javascript find html <tr> with class and with nested <td>

  15. 15

    Javascript inheriting properties and methods from a class?

  16. 16

    how to pass init parameter to base class in javascript

  17. 17

    Issues with calling internal functions in my javascript 'class'

  18. 18

    Vanilla Javascript - add Class-name to an element

  19. 19

    Javascript check if array item has class error

  20. 20

    Javascript Class, var undefined in event listener

  21. 21

    Javascript accessing class variables from window object

  22. 22

    Why Flux can use "class" in Javascript?

  23. 23

    JavaScript class & jQuery loop variable reference

  24. 24

    Dynamic creation of element and class implementation in javascript

  25. 25

    Javascript - how to define class factory as Extjs style?

  26. 26

    set attribute for dynamically created class not working in javascript

  27. 27

    JavaScript 개체의 [[Class]] 속성

  28. 28

    JavaScript: Add prototype methods to inner class

  29. 29

    Store Each Value of a AJAX Query Result to Javascript Variables

뜨겁다태그

보관