3
Hi, I was given a correct answer in a very similar question, but it lacked this detail that I can not solve...how to add the elements of the same class of a group of tables...
Example:
Jsfiddle
HTML:
<table>
<tr>
<td id="tabela">
<input type="text" id="tabelainput" class="janmembros" maxlength="3" name="janmembros" value="1" disabled>
</td>
<td id="tabela">
<input type="text" id="tabelainput" class="janvisitantes" maxlength="3" name="janvisitantes" value="3" disabled>
</td>
<td id="tabela3" class="jantotal">
</td>
</tr>
</table>
<table>
<tr>
<td id="tabela">
<input type="text" id="tabelainput" class="janmembros" maxlength="3" name="janmembros" value="1" disabled>
</td>
<td id="tabela">
<input type="text" id="tabelainput" class="janvisitantes" maxlength="3" name="janvisitantes" value="2" disabled>
</td>
<td id="tabela3" class="jantotal">
</td>
</tr>
</table>
<table>
<tr>
<td id="tabela">
<input type="text" id="tabelainput" class="janmembros" maxlength="3" name="janmembros" value="1" disabled>
</td>
<td id="tabela">
<input type="text" id="tabelainput" class="janvisitantes" maxlength="3" name="janvisitantes" value="5" disabled>
</td>
<td id="tabela3" class="jantotal">
</td>
</tr>
</table>
<table>
<tr>
<td id="tabela">
</td>
<td id="tabela">
</td>
<td id="tabela3" class="total">
</td>
</tr>
</table>
Jquery:
if ( $( ".janmembros" ).val() != '' && $( ".janvisitantes" ).val() != '' )
{
$('table').each(function() {
var $this = $(this),
janmembros = parseInt($this.find( ".janmembros" ).val()),
janvisitantes = parseInt($this.find( ".janvisitantes" ).val());
$this.find( ".jantotal" ).html( janmembros+janvisitantes );
$( ".total" ).html($this.find( ".jantotal" ).html()); // essa linha deveria somar todos as classes .jantotal, mas só soma a última...
});
}
EDIT:
After the help of @Sergio I still could not solve the problem, follow Jsfiddle with new information of the problem: Jsfiddle
There are two Ivs with the tables inside and I need the sum of each of the Ivs...I need to jump to the next sum of the next div !
and to add up the value of all of the same class ?
– Alan PS
@Alanps, but you want to add one to one, right?
– Sergio
No, I want the total value of all items in the same class...!
– Alan PS
@Alanps, exactly, one by one, class by class. It’s the example I put. I’m going to add this idea to your code and put it here
– Sergio
@Alanps: http://jsfiddle.net/6g6Wd/6/
– Sergio
that’s right, there’s only one detail, I need to add the "jantotal" field and if I change the class in your jsfiddle example it doesn’t work...
– Alan PS
@Alanps either uses my simple example in the answer, after the code you already have, or you can use it like this: http://jsfiddle.net/6g6Wd/7/
– Sergio
ah, Valew @Sergio, I got it, I just changed line 7 of the example to: somatorio += parseint($this.find('.jantotal').html()) || 0; http://jsfiddle.net/6g6Wd/9/
– Alan PS
@Alanps, this version you put up works but it’s not good. Take a look at my last comment above. You already have that amount, you don’t need to go do another
.find()
which only delays everything. And always use parseint with the second parameter, unless you want to convert numbers with octal basis.– Sergio
and in that case ? http://jsfiddle.net/6g6Wd/10/
– Alan PS
@Alanps, this is much better!
– Sergio
but how to catch the result of the second div dai ?
– Alan PS
@Alanps better explain what 2a div means
– Sergio
in this case http://jsfiddle.net/6g6Wd/13/, there are two Ivs with the tables inside and need the sum of each of the Ivs...I need to jump to the next sum of the next div
– Alan PS