Ver Mensaje Individual
  #11 (permalink)  
Antiguo 23/01/2012, 21:42
Avatar de Nemutagk
Nemutagk
Colaborador
 
Fecha de Ingreso: marzo-2004
Ubicación: México
Mensajes: 2.633
Antigüedad: 20 años
Puntos: 406
Respuesta: Funciones que calculan al principio pero luego dan resultados errados

Bueno, primero, tendrás que agregar un "indice" a todos los id's de los campos (ojo, esto no afecta al PHP que lo va a procesar, php lee el name no el id), el "indice" será compartido por todos los campos de una sola fila, para la siguiente se sumara 1 al indice para obtener el siguiente numero, el javascript es muy sencillo, prueba y me cuentas

Código PHP:
Ver original
  1. <html>
  2.     <head>
  3.         <title>Hola mundo</title>
  4.         <script type="text/javascript">
  5.            
  6.             function ingresos(fila) {
  7.                 var dias = document.getElementById('dias' + fila).value;
  8.                 var sueldo = document.getElementById('sueldo' + fila).value;
  9.                 dias = parseInt(dias);
  10.                 sueldo = parseFloat(sueldo).toFixed(2);
  11.                 var total = dias + sueldo;
  12.                 return total;
  13.             }
  14.            
  15.             function descuentos(fila) {
  16.                 var desc = document.getElementById('descuentos' + fila).value;
  17.                 var ivvs = document.getElementById('ivvs' + fila).value;
  18.                 var paro = document.getElementById('paro' + fila).value;
  19.                 var lph = document.getElementById('lph' + fila).value;
  20.                
  21.                 desc = parseFloat(desc);
  22.                 desc = parseFloat(ivvs);
  23.                 desc = parseFloat(paro);
  24.                 desc = parseFloat(lph);
  25.                
  26.                 var total = desc + ivvs + paro + lph;
  27.                
  28.                 return total;
  29.             }
  30.            
  31.             function sumar(fila) {
  32.                 var ing = ingresos(fila);
  33.                 var des = descuentos(fila);
  34.                
  35.                 ing = parseFloat(ing).toFixed(2);
  36.                 des = parseFloat(des).toFixed(2);
  37.                
  38.                 var total = ing - des;
  39.                 total = parseFloat(total).toFixed(2);
  40.                
  41.                 document.getElementById('total' + fila).value = total;
  42.             }
  43.            
  44.             function todo() {
  45.                 var campos = document.getElementById('campos').value;
  46.                 for(var i=0; i<campos; i++) {
  47.                     sumar(i);
  48.                 }
  49.             }
  50.         </script>
  51.     </head>
  52.     <body>
  53.         <form id="miform" method="post">
  54.             <table>
  55.                 <tr>
  56.                     <td>dias</td>
  57.                     <td>sueldo dia</td>
  58.                     <td>ivvs</td>
  59.                     <td>paro</td>
  60.                     <td>lph</td>
  61.                     <td>descuento</td>
  62.                     <td>ingresos</td>
  63.                 </tr>
  64.                 <?php
  65.                 for($i=0; $i<10; $i++) {
  66.                     echo '<tr>'.PHP_EOL;
  67.                     echo '<td><input type="text" id="dias'.$i.'" name="dias[]" value="'.rand(1,99).'" /></td>';
  68.                     echo '<td><input type="text" id="sueldo'.$i.'" name="sueldo[]" value="'.rand(1,99).'.'.rand(1,99).'" /></td>';
  69.                     echo '<td><input type="text" id="ivvs'.$i.'" name="ivvs" value="'.rand(1,99).'.'.rand(1,99).'" /></td>';
  70.                     echo '<td><input type="text" id="paro'.$i.'" name="paro" value="'.rand(1,99).'.'.rand(1,99).'" /></td>';
  71.                     echo '<td><input type="text" id="lph'.$i.'" name="lph" value="'.rand(1,99).'.'.rand(1,99).'" /></td>';
  72.                     echo '<td><input type="text" id="descuentos'.$i.'" name="descuentos" value="'.rand(1,99).'.'.rand(1,99).'" /></td>';
  73.                     echo '<td><input type="text" id="ingresos'.$i.'" name="ingresos" value="'.rand(1,99).'.'.rand(1,99).'" /></td>';
  74.                     echo '<td><input type="text" id="total'.$i.'" name="total[]" /></td>';
  75.                     echo '<td><input type="button" value="calcular" onclick="sumar(\''.$i.'\')" /></td>';
  76.                     echo '</tr>';
  77.                 }
  78.                 ?>
  79.             </table>
  80.             <input type="hidden" id="campos" value="<?php echo $i; ?>" />
  81.             <p><input type="button" value="calcular todo" onclick="todo()" /></p>;
  82.         </form>
  83.     </body>
  84. </html>
__________________
Listo?, tendría que tener 60 puntos menos de IQ para considerarme listo!!!
-- Sheldon Cooper
http://twitter.com/nemutagk
PD: No contestaré temas vía mensaje personal =)