Function to total input values selected in checkbox

1

Good afternoon.

I have a table on my page with columns of quantity and value of the product. These values are returned from my BD, in each line has a checkbox for selection, I have already done a function to calculate the total of each product (quantity * value). What I need is a function to total the sum of the products selected by the checkbox.

HTML / PHP

<table id="produtos" class="table table-striped table-condensed table-bordered  table-hover">

  <thead>
    <tr>
      <th style="width:10%">Produto</th>
      <th style="width:30%">Descricao</th>
      <th>Quantidade</th>
      <th>Valor Tabela</th>
      <th>Ultima Compra</th>
      <th style="width:5%">Emb.</th>
      <th style="width:5%">Peso Bruto</th>
      <th style="width:5%">Est. Casa</th>
      <th style="width:10%">Est. Cliente</th>
      <th style="width:10%">Qtd. Pedido</th>
      <th style="width:10%">Valor compra</th>
      <th style="width:20%">Total</th>
      <th>Sel.</th>
    </tr>
  </thead>
  <tbody>
    <?php $i = 0; 
      do {  ?>
    <tr>
      <td><?php echo $row_vendas['produto']; ?><input type="hidden" name="produto[]" value="<?php echo $row_vendas['id_produto'] ?>"></td>
      <td><?php echo $row_vendas['descricao']; ?><input type="hidden" name="descricao[]" value="<?php echo $row_vendas['descricao'] ?>"></td>
      <td><?php echo number_format($row_vendas['quantidade'],3,',','.'); ?></td>
      <td><?php echo number_format($row_vendas['preco1'],2,',','.'); ?></td>
      <td><?php echo date('d/m/Y', strtotime($row_vendas['ultima_compra'])); ?></td>
      <td><?php echo $row_vendas['embalagem']; ?></td>
      <td><?php echo $row_vendas['peso_bruto']; ?><input type="hidden" name="peso[]" value="<?php echo $row_vendas['peso_bruto'] ?>"></td>
      <td><?php echo number_format($row_vendas['estoque'],3,',','.'); ?></td>
      <td><input type="number" min="1" name="estoque" style="width:80%" autocomplete="off"></td>
      <td><input type="number" min="1" name="qtd[]" id="qtd<?php echo $i ?>" style="width:80%" autocomplete="off" onChange="somaProduto(<?php echo $i; ?>)"></td>
    <td>
      <input type="text" name="valor[]" id="preco<?php echo $i; ?>" style="width:70%" value="<?php echo number_format($row_vendas['valor'],2,',','.') ?>" autocomplete="off" onkeypress="return SomenteNumero(this, event);" onChange="somaProduto(<?php echo $i; ?>)">
      <input type="hidden" id="tolerancia<?php echo $i; ?>" style="width:80%" value="<?php echo $row_vendas['tolerancia_preco'] ?>" autocomplete="off">
      <input type="hidden" id="valTabela<?php echo $i; ?>" style="width:80%" value="<?php echo $row_vendas['preco1'] ?>" autocomplete="off">
    </td>
    <td><input type="text" name="totalProduto" disabled class="disabled" id="totalProduto<?php echo $i?>" style="width:80%" autocomplete="off"></td>
    <td><input type="checkbox" onClick="SomaTotais(<?php echo $i; ?>)" name="selecao[]" id="selecao<?php echo $i?>" value="<?php echo $i; ?>" style="width: 30px; height: 30px;"></td>              
  </tr>
  <?php $i = $i+1; } while ($row_vendas = mysql_fetch_assoc($vendas));?>
</tbody>
</table>

JavaScript

function somaProduto(nro){
    var qtd = $('#qtd'+nro+'').val();
    var valUnit = DesFormataMoeda($('#preco'+nro).val());

    total   = qtd * valUnit;

    $('#totalProduto'+nro+'').val(ConverteMoeda(total));
}

I have done this function, but it is not yet adding the values:

function SomaTotais(nro){
    var total = 0;
    $("input:checkbox[data-id=selecao]:checked").each(function () {
            var i = this.value;
            var valor = DesFormataMoeda($('#totalProduto'+i).val());                    
            total += valor              
        });
        alert(ConverteMoeda(total));

}
    
asked by anonymous 21.10.2015 / 14:02

1 answer

2

Solved here, changed function and worked as intended.

function SomaTotais(){
	var total = 0;
	$("input:checkbox[data-id=selecao]:checked").each(function () {
		var i = this.value;
        var valor = Number(DesFormataMoeda($('#totalProduto'+i).val()));						
		total += valor;
		$('#total').html(total);				
	});			
}
    
21.10.2015 / 20:04