Foros del Web » Programando para Internet » Javascript »

suma y resta de text box con la misma funcion

Estas en el tema de suma y resta de text box con la misma funcion en el foro de Javascript en Foros del Web. Buenas tardes . nuevamente necesito de su colaboracion necesito sumar y restar los datos que ingresen en los text boxestos text box son dinamicos dependen ...
  #1 (permalink)  
Antiguo 17/03/2009, 14:05
 
Fecha de Ingreso: abril-2008
Mensajes: 28
Antigüedad: 16 años
Puntos: 0
suma y resta de text box con la misma funcion

Buenas tardes .

nuevamente necesito de su colaboracion necesito sumar y restar los datos que ingresen en los text boxestos text box son dinamicos dependen de la cantidad de registros q se encuentren el la bd hasta ahora lo que he podido hacer es que sumen tengo algo como esto.
Código HTML:
function sumar(campo)
{
	document.formulario.tmain.value=parseInt(document.formulario.tmain.value)+parseInt(campo.value);
}
	<td align="center">
	<span class="style1">
	<input type="text" name="capacidad<? echo $a?>" size="12" value="<? echo $row1["Capacidad"];?>" onBlur="sumar(this);">&nbsp;
	<input type="hidden" name="valorantiguo<? echo $a?>" value="<? echo $row1["Capacidad"];?>">
	</span>
	</td> 
agradesco cualquier ayuda q me puedan brindar
  #2 (permalink)  
Antiguo 17/03/2009, 14:23
 
Fecha de Ingreso: marzo-2009
Mensajes: 120
Antigüedad: 15 años, 1 mes
Puntos: 1
Respuesta: suma y resta de text box con la misma funcion

se mas claro con tus preguntas, y pon el codigo completo, que es $a?
valor antiguo y capacidad valen lo mismo? <? echo $row1["Capacidad"];?>,

si estas llamando la funcion -> onBlur="sumar(this); porque no usas this.value dentro de la funcion.

que quieres sumar, que quieres restar???... tomate tu tiempo al redactar una pregunta, asi depronto te puedan ayudar.
  #3 (permalink)  
Antiguo 17/03/2009, 14:33
 
Fecha de Ingreso: abril-2008
Mensajes: 28
Antigüedad: 16 años
Puntos: 0
Respuesta: suma y resta de text box con la misma funcion

Código:
<html>
<head>
<title>Circuitos</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="javascript" script type="text/javascript">
function sumar(campo)
{
	document.formulario.tmain.value=parseInt(document.formulario.tmain.value)+parseInt(campo.value);
}
function sumara(campo)
{
	document.formulario.tstandby.value=parseInt(document.formulario.tstandby.value)+parseInt(campo.value);
}
function validar()
{
 var error = '';
 var d = document.formulario;
   if( parseInt(d.tmain.value) > parseInt(d.operacion.value) ) error += '\n*Se ha superado la capacidad breaker alimentación barraje (MAIN)';
   if( parseInt(d.tstandby.value) > parseInt(d.operacion1.value) ) error += '\n*Se ha superado la capacidad breaker alimentación barraje (STANDBY)';
   if( error == '' )
   { 
   d.submit();
   window.opener.location.reload();
   window.close();
   }
   else alert(error);
}
</script>
<style type="text/css">
<!--
.style1 {color: #FFFFFF}
-->
</style>
</head>
<body bgcolor="#3466BB">
<?
//CONEXION CON LA BASE DE DATOS
mysql_connect("","","");
//SELECCIÓN DE LA BASE DE DATOS A TRABAJAR
mysql_select_db("Potencia");
$selecta="SELECT Num_Circuitos FROM pdb WHERE Id_PDB='$idpdb'";
$querya=mysql_query($selecta);
$rowa=mysql_fetch_array($querya);
$totala=$rowa[0];		
$update="UPDATE PDB SET Cap_Breaker_Alim_Barraje='$capacidad',Cal_Acom_Breaker_Proteccion='$calibre',Num_Circuitos='$circuitos',Cap_Breaker_Alim_Barraje_S='$capacidads',Cal_Acom_Breaker_Proteccion_S='$calibres' WHERE Id_PDB='$idpdb' AND Sitio_Id_Sitio=$idsitio";
$query=mysql_query($update);
$select="SELECT Num_Circuitos FROM pdb WHERE Id_PDB='$idpdb'";
$query2=mysql_query($select);
$row2=mysql_fetch_array($query2);
$total=$row2[0];	
$count="SELECT COUNT(PDB_Id_PDB) FROM circuito WHERE PDB_Id_PDB='$idpdb'";
$query1=mysql_query($count);
$row1=mysql_fetch_array($query1);
$total1=$row1[0];
if($total<$total1)
{
	$totalb=$totala-$total;
	$totalc=$totalb/2;
	$totald=$totalb/2;
	for($j=0;$j<$totalc;$j++)
	{
	$update="UPDATE circuito SET power_Id_Power='$power1' WHERE pdb_Id_PDB='$idpdb' AND pdb_sitio_Id_Sitio='$idsitio' AND Barrajes='Main'";
	$consulta=mysql_query($update);	
	$select="SELECT Id_Circuito,Barrajes FROM circuito WHERE PDB_Id_PDB='$idpdb' AND Barrajes='Main' ORDER BY Id_Circuito DESC";
	$query=mysql_query($select);
	$row=mysql_fetch_array($query);
	$idcircuito=$row[0];
	$delete="DELETE FROM circuito WHERE Id_Circuito='$idcircuito'";
	$query1=mysql_query($delete);
	}
	for($k=0;$k<$totald;$k++)
	{
	$update1="UPDATE circuito SET power_Id_Power='$power2' WHERE pdb_Id_PDB='$idpdb' AND pdb_sitio_Id_Sitio='$idsitio' AND Barrajes='Standby'";
	$consulta1=mysql_query($update1);		
	$select="SELECT Id_Circuito,Barrajes FROM circuito WHERE PDB_Id_PDB='$idpdb' AND Barrajes='Standby' ORDER BY Id_Circuito DESC";
	$query=mysql_query($select);
	$row=mysql_fetch_array($query);
	$idcircuito=$row[0];
	$delete="DELETE FROM circuito WHERE Id_Circuito='$idcircuito'";
	$query1=mysql_query($delete);
	}	
}
elseif($total==$total1)
{
	$update="UPDATE circuito SET power_Id_Power='$power1' WHERE pdb_Id_PDB='$idpdb' AND pdb_sitio_Id_Sitio='$idsitio' AND Barrajes='Main'";
	$consulta=mysql_query($update);	
	$update1="UPDATE circuito SET power_Id_Power='$power2' WHERE pdb_Id_PDB='$idpdb' AND pdb_sitio_Id_Sitio='$idsitio' AND Barrajes='Standby'";
	$consulta1=mysql_query($update1);		
}
elseif($total>$total1)
{
	$total2=$total-$total1;
	$total3=$total2/2;
	$total4=$total2/2;
	$update1="UPDATE circuito SET power_Id_Power='$power1' WHERE pdb_Id_PDB='$idpdb' AND pdb_sitio_Id_Sitio='$idsitio' AND Barrajes='Main'";
	$consulta1=mysql_query($update1);		
	$select="SELECT Posicion FROM circuito WHERE PDB_Id_PDB='$idpdb' AND Barrajes='Main' ORDER BY Posicion ASC";
	$query=mysql_query($select);
	$row=mysql_fetch_array($query);
	$posicion=$row[0];
	$selecta="SELECT Id_Power FROM power WHERE sitio_Id_Sitio='$idsitio'";
	$querya=mysql_query($selecta);
	$rowa=mysql_fetch_array($querya);
	$idpower=$rowa[0];	
	for($h=0;$h<$total3;$h++)
	{
		$posicion++;
		$insert="INSERT INTO circuito (pdb_sitio_Id_Sitio,PDB_Id_PDB,power_Id_Power,Posicion,Estado,Capacidad,Barrajes,Observacion) VALUES ('$idsitio','$idpdb','$power1','$posicion','DISPONIBLE',0,'Main','')";
		$query3=mysql_query($insert);
	}
	$update="UPDATE circuito SET power_Id_Power='$power2' WHERE pdb_Id_PDB='$idpdb' AND pdb_sitio_Id_Sitio='$idsitio' AND Barrajes='Standby'";
	$consulta=mysql_query($update);			
	$select1="SELECT Posicion FROM circuito WHERE PDB_Id_PDB='$idpdb' AND Barrajes='Standby' ORDER BY Posicion ASC";
	$query1=mysql_query($select1);
	$row1=mysql_fetch_array($query1);
	$posicion1=$row1[0];															
	for($l=0;$l<$total4;$l++)
	{
		$posicion1++;
		$insert="INSERT INTO circuito (pdb_sitio_Id_Sitio,PDB_Id_PDB,power_Id_Power,Posicion,Estado,Capacidad,Barrajes,Observacion) VALUES ('$idsitio','$idpdb','$power2','$posicion1','DISPONIBLE',0,'Standby','')";
		$query3=mysql_query($insert);
	}
}
else
{
}
?>
<form method="post" action="Pdb7.php" name="formulario" id="formulario">
<table width="90%" border="1" align="center" cellpadding="2" cellspacing="2" bgcolor="#000099">
<tr>
<th colspan="5" align="center"><span class="style1">N&uacute;mero PDB <? echo $pdb?>&nbsp;</span></th>
</tr>
<tr>
<th><span class="style1">Barraje</span></th>
<th><span class="style1">Posici&oacute;n</span></th>
<th><span class="style1">Estado </span></th>
<th><span class="style1">Capacidad (A)</span></th>
<th><span class="style1">Observaci&oacute;n</span></th>
</tr>
<?
$select="SELECT SUM(Num_Circuitos) FROM PDB WHERE Sitio_Id_Sitio='$idsitio' AND Id_PDB='$idpdb'";
$query=mysql_query($select);
$row=mysql_fetch_array($query); 
$circuitos=$row[0];
$select1="SELECT * FROM circuito WHERE PDB_Id_PDB='$idpdb' order by Barrajes ";
$query1=mysql_query($select1);
$count="SELECT SUM(Capacidad) FROM circuito WHERE PDB_Id_PDB='$idpdb' AND Barrajes='Main'";
$consulta=mysql_query($count);
$fila=mysql_fetch_array($consulta);
$count1="SELECT SUM(Capacidad) FROM circuito WHERE PDB_Id_PDB='$idpdb' AND Barrajes='Standby'";
$consulta1=mysql_query($count1);
$fila1=mysql_fetch_array($consulta1);
for ($i=1; $i<=$circuitos; $i++)
{
	$row1=mysql_fetch_array($query1);
	$a++;
	?>
	<input type="hidden" name="idcircuito<? echo $a?>" value="<? echo $row1["Id_Circuito"];?>">
	<tr>
	<td align="center"><span class="style1"><? echo $row1["Barrajes"];?>&nbsp;</span></td>	
	<td align="center"><span class="style1"><? echo $row1["Posicion"];?>&nbsp;</span></td>
	<td align="center"><span class="style1"><select name="estado<? echo $a?>"><option selected><? echo $row1["Estado"];?></option><option>EN USO</option><option>DISPONIBLE</option></select>&nbsp;</span></td>
	<?
	if($row1["Barrajes"]=="Main")
	{
	?>
	<td align="center">
	<span class="style1">
	<input type="text" name="capacidad<? echo $a?>" size="12" value="<? echo $row1["Capacidad"];?>" onBlur="sumar(this);">&nbsp;
	
	</span>
	</td>
	<?
	}
	else
	{
	?>
	<td align="center">
	<span class="style1">
	<input type="text" name="capacidades<? echo $a?>" size="12" value="<? echo $row1["Capacidad"];?>" onBlur="sumara(this);">&nbsp;
		
	</span>
	</td>
	<?
	}
    ?>
	<td><span class="style1"><textarea name="observacion<? echo $a?>" cols="45" rows="1"><? echo $row1["Observacion"];?></textarea>&nbsp;</span></td>
	</tr>
	<?
}	
?>
<input type="hidden" name="contador" value="<? echo $a?>">
<tr>
<td colspan="4" align="center">
<span class="style1">Acumulado:
<input type="text" name="tmain" size="12" value="<? echo $fila[0];?>" ReadOnly>
</span>
<span class="style1">Total main:
<input type="text" name="operacion" size="12" value="<? echo $capacidad;?>" ReadOnly>
</span>
</td>
<td  align="center">
<span class="style1">Acumulado:
<input type="text" name="tstandby" size="12" value="<? echo $fila1[0];?>" ReadOnly>
</span>
<span class="style1">Total standby&nbsp;
<input type="text" name="operacion1" size="12" value="<? echo $capacidads;?>" ReadOnly>&nbsp;
</span>
</td>
</tr>
<tr>
<td colspan="5" align="center"><input type="button" onclick="validar();" value="Aceptar" /></td>
</tr>
</table>
</form>
</body>
</html>
bueno creo q me acelere un poco pero es q me estan precionando y no he podido, lo que pasa es q necesito q a medida q vayan ingresando valores a los text box se vayan sumando pero que si se pasan de un valor q traigo de la bd pues que le permita al usuario ir a un text box y que le permita restar yo lo q hasta ahora tengo es q se sumen los valores de los text box pero si se pasan y quieren restar lo q hace es seguir sumando el valor que le ingresan de nuevo .

Última edición por Jahir; 17/03/2009 a las 15:06
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 08:43.