Foros del Web » Programando para Internet » Javascript »

Función para Sumar Arreglo

Estas en el tema de Función para Sumar Arreglo en el foro de Javascript en Foros del Web. Estimados, recurro a ustedes para que me puedan orientar un poco. Resulta que tengo un formulario Dinamico donde se generan 1 a N cantidad de ...
  #1 (permalink)  
Antiguo 19/07/2013, 08:30
 
Fecha de Ingreso: junio-2013
Ubicación: Santiago
Mensajes: 13
Antigüedad: 10 años, 10 meses
Puntos: 0
Función para Sumar Arreglo

Estimados,
recurro a ustedes para que me puedan orientar un poco.
Resulta que tengo un formulario Dinamico donde se generan 1 a N cantidad de input dependiendo de los dias del Més. Quiere decir que se generan desde 1° fecha del mes hasta la fecha de hoy, una cantidad X de input.

Mi formulario es algo similar a esto.
Código PHP:
<form  name="form" action="xxx.php" method="POST" >
                                 
                  <tr>
                    
                    <td align="center" colspan="2" ><strong>SINIESTRADOS</strong></td>
                    <td align="center" colspan="2" ><strong>PAGADO</strong></td>
                    <td align="center" colspan="2" ><strong>RECHAZADOS</strong></td>
                    <td align="center" colspan="2" ><strong>PROVISIONADO ANTERIOR</strong></td>
                  </tr>
                                   
                  <tr>
                   
                    <td width="104" colspan="1" align="center" ><strong>Pendiente</strong></td>
                    <td width="104" colspan="1" align="center" ><strong>Provisionado</strong></td>
                    <td width="83" colspan="1" align="center" ><strong>Q</strong></td>
                    <td width="83" colspan="1" align="center" ><strong>$</strong></td>
                    <td width="83" colspan="1" align="center" ><strong>Q</strong></td>
                    <td width="83" colspan="1" align="center" ><strong>$</strong></td>
                    <td width="83" colspan="1" align="center" ><strong>Q</strong></td>
                    <td width="85" colspan="1" align="center" ><strong>$</strong></td>
                  </tr>
                 
                  
            
                <?
                
while ($day<$date):
                
$fecha $year."-".$month."-".$day;
                
$fechahoy date("Y-m-d");
                if(
$fecha == $fechahoy)
                    break;
                    
                
?>
                <tr>
               
                        <td><? echo $day ?></td>
                        <td><input style="width: 80px" type="text" name="pendientes"/></td>
                        <td><input style="width: 80px" type="text" name="provisionado[]"/></td>
                        <td><input style="width: 80px" type="text" name="siniestrado_q[]"/></td>
                        <td><input style="width: 80px" type="text" name="siniestrado_s[]"/></td>
                        <td><input style="width: 80px" type="text" name="pagado_q[]"/></td>
                        <td><input style="width: 80px" type="text" name="pagado_s[]"/></td>
                        <td><input style="width: 80px" type="text" name="rechazos_q[]"/></td>
                        <td><input style="width: 80px" type="text" name="rechazos_s[]"/></td>
                    </tr>
                                
                <? 
                
                $day
++;
                
$off++;
                                             
                endwhile;
                                
                
?>
                <tr>
            

            <td colspan="2" align="left"><input type="submit" value="Enviar" class="submit"/></td>
            
            </tr>
           
                   
                   
                </table>
                </form>

Lo que necesito es hacer una función javascript que primero me permita leer todos los valores del arreglo de los Input, luego sumarlos y restarlos y mostrarlo de forma instantanea en otro campo llamado total. la operación matematica seria similar a provisionado[] + siniestro_s[] - pagados_S - rechazos_s.

Estube buscando ejemplos y este se acomoda mas a mis necesidades pero no logro como implementar dicha función ya que no tengo muchos conocimientos de JS

http://fivemedia.com.ar/dexter/test221.php
  #2 (permalink)  
Antiguo 19/07/2013, 10:01
 
Fecha de Ingreso: junio-2013
Ubicación: Santiago
Mensajes: 13
Antigüedad: 10 años, 10 meses
Puntos: 0
Respuesta: Función para Sumar Arreglo

Código PHP:
<?
                
                $date
=01;
                
$day=01;
                
$off=0;
                 
                
                
$month '07';
                
$year '2013';
                 
                
                while (
checkdate($month,$date,$year)):
                
$date++;
                endwhile;
                
?>
                
                <form id="controlprovisiones" class="formular" name="controlprovisiones" action=" " method="POST" >
                <div id="a" class="prueba"><table width="600" border="0" class ="tabla">
                 
                  <tr>
                    <td align="center" colspan="3" ><strong>prueba</strong></td>
                    <td align="center" colspan="2" ><strong>SINIESTRADOS</strong></td>
                    <td align="center" colspan="2" ><strong>PAGADO</strong></td>
                    <td align="center" colspan="2" ><strong>RECHAZADOS</strong></td>
                    <td align="center" colspan="2" ><strong>PROVISIONADO ANTERIOR</strong></td>
                  </tr>
                  <tr><td align="right" colspan="10" ><strong>231455</strong></td></tr>
                   <tr></tr><tr></tr><tr></tr> <!--Solo son saltos de lineas para que se vea mas ordenado --> 
                  <tr></tr><tr></tr><tr></tr> <!--Solo son saltos de lineas para que se vea mas ordenado -->
                  <tr>
                    <td width="52" colspan="1" align="left" ><strong>Julio</strong></td>
                    <td width="104" colspan="1" align="center" ><strong>Pendiente</strong></td>
                    <td width="104" colspan="1" align="center" ><strong>Provisionado</strong></td>
                    <td width="83" colspan="1" align="center" ><strong>Q</strong></td>
                    <td width="83" colspan="1" align="center" ><strong>$</strong></td>
                    <td width="83" colspan="1" align="center" ><strong>Q</strong></td>
                    <td width="83" colspan="1" align="center" ><strong>$</strong></td>
                    <td width="83" colspan="1" align="center" ><strong>Q</strong></td>
                    <td width="85" colspan="1" align="center" ><strong>$</strong></td>
                  </tr>
                 
                  <!--Start the table data and make sure the number of days does not exceed
                    the total for the month  - $date will always be one more than the total
                    number of days in the momth  -->
            
                <?
                
while ($day<$date):
                
$fecha $year."-".$month."-".$day;
                
$fechahoy date("Y-m-d");
                if(
$fecha == $fechahoy)
                    break;
                    
                    
                
?>
                <script language="javascript">
  
       function validar()
    {
        var ref1 = document.getElementById("provisionado");
        var ref2 = document.getElementById("siniestrado");
        var ref3 = document.getElementById("pagado");
        var ref4 = document.getElementById("rechazos");
        var ref5 = document.getElementById("subtotal");     
 
        if(ref1.value>0 && ref2.value>0 && ref3.value>0 && ref4.value>0)
        {
     ref5.value = parseFloat(ref1.value) +  parseFloat(ref2.value) -  parseFloat(ref3.value) -  parseFloat(ref4.value);
        }
    }
</script>
                <tr>
               
                    <td><? echo $day ?></td>
                    <td><input style="width: 80px" type="text" name="pendientes"/></td>
                    <td><input style="width: 80px" type="text" name="provisionado[]" class="provisionado" id="provisionado" onchange="validar();"/></td>
                    <td><input style="width: 80px" type="text" name="siniestrado_q[]"/></td>
                    <td><input style="width: 80px" type="text" name="siniestrado_s[]" class="siniestrado" id="siniestrado" onchange="validar();"/></td>
                    <td><input style="width: 80px" type="text" name="pagado_q[]"></td>
                    <td><input style="width: 80px" type="text" name="pagado_s[]" class="pagado" id="pagado" onchange="validar();"/></td>
                    <td><input style="width: 80px" type="text" name="rechazos_q[]"/></td>
                    <td><input style="width: 80px" type="text" name="rechazos_s[]" class="rechazos" id="rechazos" onchange="validar();"/></td>
                    <td colspan="2" align="left"><label for="total">subTotal: <input type="text" name="subtotal" id="subtotal"/></td>
                    </tr>
            
                    
                <? 
                
/*  Increment the day and increment the cells that go before the end of the row
                    and end the row when appropriate */
                
$day++;
                
$off++;
                 
                                 
                endwhile;
                
                
                
?>
                <tr>
            
            
             <td colspan="5" align="right"><input type="text" name="total" id="total" value="0"/></td>
            </tr>
           
                   
                   
                </table></div></form>
Esta función hace lo que necesito pero solo con la primera fila, con las demas filas no realiza la función validar.

Que podria estar mal, al parecer tengo que recorrer los valores de los Input con un For en la Función Javascript.

Alguien que me ayudeee
  #3 (permalink)  
Antiguo 19/07/2013, 10:49
 
Fecha de Ingreso: junio-2013
Ubicación: Santiago
Mensajes: 13
Antigüedad: 10 años, 10 meses
Puntos: 0
Respuesta: Función para Sumar Arreglo

Lo solucione
<script language="javascript">

function validar<?php echo $day; ?>()
{
var ref1 = document.getElementById("provisionado<?php echo $day; ?>");
var ref2 = document.getElementById("siniestrado<?php echo $day; ?>");
var ref3 = document.getElementById("pagado<?php echo $day; ?>");
var ref4 = document.getElementById("rechazos<?php echo $day; ?>");
var ref5 = document.getElementById("subtotal<?php echo $day; ?>");

if(ref1.value>=0 && ref2.value>=0 && ref3.value>=0 && ref4.value>=0)
{
ref5.value = parseFloat(ref1.value) + parseFloat(ref2.value) - parseFloat(ref3.value) - parseFloat(ref4.value);
}
}
</script>


<td><? echo $day ?></td>
<td><input style="width: 80px" type="text" name="pendientes" value="0"/></td>
<td><input style="width: 80px" type="text" name="provisionado[]" class="provisionado" id="provisionado<?php echo $day; ?>" onchange="validar<?php echo $day; ?>();" value="0"/></td>
<td><input style="width: 80px" type="text" name="siniestrado_q[]" value="0"/></td>
<td><input style="width: 80px" type="text" name="siniestrado_s[]" class="siniestrado" id="siniestrado<?php echo $day; ?>" onchange="validar<?php echo $day; ?>();" value="0"/></td>
<td><input style="width: 80px" type="text" name="pagado_q[]" value="0"></td>
<td><input style="width: 80px" type="text" name="pagado_s[]" class="pagado" id="pagado<?php echo $day; ?>" onchange="validar<?php echo $day; ?>();" value="0"/></td>
<td><input style="width: 80px" type="text" name="rechazos_q[]" value="0"/></td>
<td><input style="width: 80px" type="text" name="rechazos_s[]" class="rechazos" id="rechazos<?php echo $day; ?>" onchange="validar<?php echo $day; ?>();" value="0"/></td>
<td colspan="2" align="left"><label for="total">subTotal: <input type="text" name="subtotal" id="subtotal<?php echo $day; ?>"/></td>
</tr>

Etiquetas: arreglo, formulario, input, js, php
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 20:29.