Skip to main content
3 de 4
se añadieron 1089 caracteres en el cuerpo
Kiko_L
  • 6.6k
  • 1
  • 13
  • 25

El problema es que el valor dentro de tu td no está en el html del propio td sino en un input. Tienes que acceder al children del td y luego al valor de input (o buscar el closest input, lo que mejor te venga). Tienes también que transformar el valor a número para que no te lo tome como texto y lo concatene (en este caso he usado Number()).

Te dejo un ejemplo resumido:

$(document).on('keyup','#datatable-buttons input.peso',function(){
                peso = $(this).val();
                        
                sumar();
        });

    function sumar(){
            pesostotal = 0;
            $("#datatable-buttons tr").not(':first').each(function(){
                pesostotal = pesostotal + Number($(this).find('td:eq(2)').children().val());
            });
            $('#ptotal').val(pesostotal);
        }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table id="datatable-buttons" class="table table-striped table-bordered dataTable no-footer dtr-inline" role="grid" aria-describedby="datatable-buttons_info" style="width: 100%;">
                     <thead>
<tr role="row">
  <th class="sorting_asc" tabindex="0" aria-controls="datatable-buttons" rowspan="1" colspan="1" style="width: 50px;" aria-sort="ascending" aria-label="Name: activate to sort column descending">#</th>
  <th class="sorting" tabindex="0" aria-controls="datatable-buttons" rowspan="1" colspan="1" style="width: 200px;" aria-label="Position: activate to sort column ascending">Calidad</th>
  <th class="sorting" tabindex="0" aria-controls="datatable-buttons" rowspan="1" colspan="1" style="width: 50px;" aria-label="Office: activate to sort column ascending">Peso</th>
  <th class="sorting" tabindex="0" aria-controls="datatable-buttons" rowspan="1" colspan="1" style="width: 50px;" aria-label="Age: activate to sort column ascending">Cajas</th>
  <th class="sorting" tabindex="0" aria-controls="datatable-buttons" rowspan="1" colspan="1" style="width: 50px;" aria-label="Start date: activate to sort column ascending">Precio</th>
  </tr>
  </thead>

  <tbody>
<tr role="row" class="odd">
  <td tabindex="0" class="sorting_1"><input type="hidden"></td>
  <td>
  </td>
  <td><input type="number" name="peso" placeholder="0" class="form-control peso"></td>
  <td><input type="number" name="cajas" placeholder="0" class="form-control cajas"></td>
  <td><input type="number" name="precio" placeholder="0" class="form-control precio"></td>
</tr>
<tr role="row" class="odd">
  <td tabindex="0" class="sorting_1"><input type="hidden"></td>
  <td>
  </td>
  <td><input type="number" name="peso" placeholder="0" class="form-control peso"></td>
  <td><input type="number" name="cajas" placeholder="0" class="form-control cajas"></td>
  <td><input type="number" name="precio" placeholder="0" class="form-control precio"></td>
</tr>
<tr role="row" class="odd">
  <td tabindex="0" class="sorting_1"><input type="hidden"></td>
  <td>
  </td>
  <td><input type="number" name="peso" placeholder="0" class="form-control peso"></td>
  <td><input type="number" name="cajas" placeholder="0" class="form-control cajas"></td>
  <td><input type="number" name="precio" placeholder="0" class="form-control precio"></td>
</tr>
  </tbody>
</table>
<input id="ptotal" type="text"/>

Kiko_L
  • 6.6k
  • 1
  • 13
  • 25