Foros del Web » Programando para Internet » Javascript »

[SOLUCIONADO] No me funciona calculo de variables.

Estas en el tema de No me funciona calculo de variables. en el foro de Javascript en Foros del Web. Hola, por favor podeis decirme porque no me funciona este código? @import url("http://static.forosdelweb.com/clientscript/vbulletin_css/geshi.css"); Código Javascript : Ver original <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ...
  #1 (permalink)  
Antiguo 19/03/2013, 06:33
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
No me funciona calculo de variables.

Hola, por favor podeis decirme porque no me funciona este código?

Código Javascript:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  5. <title>Documento sin título</title>
  6. <script>
  7. function fncSumar(){
  8. caja=document.forms["frm"].elements;
  9. var numero1 = Number(caja[cantidad].value);
  10. var numero2 = Number(caja[pvp].value);
  11. total=numero1*numero2;
  12. if(!isNaN(total)){
  13.     caja[total].value=numero1*numero2;
  14. }
  15. }
  16. </script>        
  17. </head>
  18. <body>      
  19. <table id="tabla">
  20. <form method="post" name="frm" id="frm">
  21. <tr class="cab_factura">
  22.      <td class="enlace10"></td>
  23.     <td class="enlace10"></td>
  24.     <td class="enlace10">Almacén</td>
  25.     <td class="enlace10">Referencia</td>
  26.     <td class="enlace10">Descripción</td>
  27.     <td class="enlace10">Cantidad</td>
  28.     <td class="enlace10">Precio</td>
  29.     <td class="enlace10">%Dto.</td>
  30.     <td class="enlace10">Total</td>
  31.     <td class="enlace10"></td>
  32.   </tr>
  33.  
  34. <tr>
  35.    <td><input name="button" type=button onclick="agregar();" value="+"  /></td>
  36.    <td><input name="button" type=button onclick="borrarUltima();" value="-"/></td>
  37.     <td ><input name="almacen" type="text" size="5"  /></td>
  38.     <td><input name="recambio" id="recambio" type="text" size="10"/></td>
  39.     <td><input name="denominacion" type="text" size="25"/></td>
  40.     <td><input name="cantidad"type="text" size="5" onkeyup="fncSumar();"  /></td>
  41.     <td><input name="pvp" type="text" size="5"  /></td>
  42.     <td><input name="descuento" type="text" size="5"  /></td>
  43.     <td><input name="total" type="text" size="5" /></td>
  44.     <td><img src="imagenes_menu/untitled.png" width="20" height="20" style="cursor:pointer" /></td>
  45.   </tr>  
  46. </table>
  47. </form>
  48. </body>
  49. </html>

Gracias
  #2 (permalink)  
Antiguo 19/03/2013, 07:52
Avatar de miguec04  
Fecha de Ingreso: agosto-2008
Ubicación: Cimitarra, Santander
Mensajes: 378
Antigüedad: 15 años, 8 meses
Puntos: 15
Respuesta: No me funciona calculo de variables.

a simple vista te puedo decir que hacen falta muchas cosas, que es "cantidad" que es "pvp" y que es realmente lo que no te sirve, porque hacen falta dos metodos "agregar()" y el metodo "borrarUltima()"
__________________
Desoftc Technology - Miguel Carmona
Creaciones Inteligentes - Cimitarra Colombia
[email protected]
http://www.desoftc.com.co
  #3 (permalink)  
Antiguo 19/03/2013, 08:14
Avatar de IsaBelM
Colaborador
 
Fecha de Ingreso: junio-2008
Mensajes: 5.032
Antigüedad: 15 años, 10 meses
Puntos: 1012
Respuesta: No me funciona calculo de variables.

no te funciona por que cuando haces referencia a los controles, el nombre de éstos, es tratado como variables. envuélvelos entre comillas
__________________
if(ViolenciaDeGénero) {alert('MUJER ASESINADA');}
  #4 (permalink)  
Antiguo 19/03/2013, 08:26
Avatar de miguec04  
Fecha de Ingreso: agosto-2008
Ubicación: Cimitarra, Santander
Mensajes: 378
Antigüedad: 15 años, 8 meses
Puntos: 15
Respuesta: No me funciona calculo de variables.

Si señor, yo pense que estaba trabajando con indices pero es que tenia mal los nombres, el codigo para que funcione correctamente es:

Código HTML:
Ver original
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3.     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  4.     <title>
  5.         Documento sin título
  6.     </title>
  7.     function fncSumar(){
  8.         caja            = document.forms["frm"].elements;
  9.        
  10.         var numero1     = Number(caja['cantidad'].value);
  11.         var numero2     = Number(caja['pvp'].value);
  12.         total           = numero1*numero2;
  13.         if(!isNaN(total)) {
  14.             caja['total'].value = numero1*numero2;
  15.         }
  16.     }
  17.    
  18. </script>        
  19. </head>
  20. <body>      
  21.     <form method="post" name="frm" id="frm">
  22.         <table id="tabla">
  23.             <tr class="cab_factura">
  24.                 <td class="enlace10"></td>
  25.                 <td class="enlace10"></td>
  26.                 <td class="enlace10">Almacén</td>
  27.                 <td class="enlace10">Referencia</td>
  28.                 <td class="enlace10">Descripción</td>
  29.                 <td class="enlace10">Cantidad</td>
  30.                 <td class="enlace10">Precio</td>
  31.                 <td class="enlace10">%Dto.</td>
  32.                 <td class="enlace10">Total</td>
  33.                 <td class="enlace10"></td>
  34.             </tr>
  35.             <tr>
  36.                 <td>
  37.                     <input name="button" type=button onclick="agregar();" value="+"  />
  38.                 </td>
  39.                 <td>
  40.                     <input name="button" type=button onclick="borrarUltima();" value="-"/>
  41.                 </td>
  42.                 <td >
  43.                     <input name="almacen" type="text" size="5"  />
  44.                 </td>
  45.                 <td>
  46.                     <input name="recambio" id="recambio" type="text" size="10"/>
  47.                 </td>
  48.                 <td>
  49.                     <input name="denominacion" type="text" size="25"/>
  50.                 </td>
  51.                 <td>
  52.                     <input name="cantidad"type="text" size="5" onkeydown="fncSumar();"  onkeyup="fncSumar();"/>
  53.                 </td>
  54.                 <td>
  55.                     <input name="pvp" type="text" size="5"  onkeydown="fncSumar();"  onkeyup="fncSumar();"/>
  56.                 </td>
  57.                 <td>
  58.                     <input name="descuento" type="text" size="5"  />
  59.                 </td>
  60.                 <td>
  61.                     <input name="total" type="text" size="5" />
  62.                 </td>
  63.                 <td>
  64.                     <img src="imagenes_menu/untitled.png" width="20" height="20" style="cursor:pointer" />
  65.                 </td>
  66.             </tr>  
  67.         </table>
  68.     </form>
  69. </body>
  70. </html>
__________________
Desoftc Technology - Miguel Carmona
Creaciones Inteligentes - Cimitarra Colombia
[email protected]
http://www.desoftc.com.co
  #5 (permalink)  
Antiguo 19/03/2013, 11:03
Avatar de satjaen  
Fecha de Ingreso: septiembre-2012
Ubicación: Jaén (Andalucía)
Mensajes: 893
Antigüedad: 11 años, 7 meses
Puntos: 10
Respuesta: No me funciona calculo de variables.

Gracias, una cosa como saco solo dos decimales en en la variable total?
Muchas garcias.

Etiquetas: calculo, funcion, html, input
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 00:16.