Get values with jQuery

1

I have a following field,

 <input name="dados[<?= $i ?>][quantidade]" class="quantidade" type="text" value="<?= isset($dados['quantidade']) ? $dados['quantidade'] : '' ?>" id="quantidade"/>

And I have a modal that when I choose a new food, it adds another field like the one above, I have to get these values with jQuery and perform a sum, how and what do I do to get all these fields?     

asked by anonymous 06.11.2014 / 15:01

1 answer

3

You can get it by using $ .each :

var soma = 0;

$('.quantidade').each(function(index) {
    soma += parseInt($(this).val());
});

alert('Soma: ' + soma);

I see that you are using the same ID quantidade for all elements, this is wrong, each element must have a unique ID.

I hope it helps!

    
06.11.2014 / 15:11