Foros del Web » Programando para Internet » Javascript »

Problema insertar numeros

Estas en el tema de Problema insertar numeros en el foro de Javascript en Foros del Web. Cita: <html> <head> <script> </script> </head> <body> <div id="principal"> <div> <input type="button" value="0"> <input type="button" value="1"> <input type="button" value="2"> <input type="button" value="3"> <input type="button" value="4"> ...
  #1 (permalink)  
Antiguo 27/01/2011, 11:53
 
Fecha de Ingreso: enero-2011
Mensajes: 14
Antigüedad: 13 años, 3 meses
Puntos: 0
Pregunta Problema insertar numeros

Cita:
<html>
<head>
<script>
</script>
</head>
<body>
<div id="principal">
<div>
<input type="button" value="0">
<input type="button" value="1">
<input type="button" value="2">
<input type="button" value="3">
<input type="button" value="4">
<input type="button" value="5">
<input type="button" value="6">
<input type="button" value="7">
<input type="button" value="8">
<input type="button" value="9">
</div>
<div>
<input class="campotexto" type="text" id="resultado">
</div>
</div>
</body>
</html>
Alguien me podria explicar como hacer una funcion para que esta tome el valor del boton y compruebe si es un numero y despues que muestre este en resultado? Estuve probando de 100 maneras distintas y no caigo en como hacerlo u.u Gracias de antemano por la ayuda.
  #2 (permalink)  
Antiguo 27/01/2011, 14:13
Avatar de stramin  
Fecha de Ingreso: marzo-2008
Ubicación: Cubil felino
Mensajes: 1.652
Antigüedad: 16 años, 1 mes
Puntos: 336
Respuesta: Problema insertar numeros

No estoy seguro de haberte entendido pero creo que es esto:

Código HTML:
Ver original
  1. </head>
  2. <div id="principal">
  3. <div>
  4. <input type="button" value="0" onClick="document.getElementById('resultado').value=this.value;">
  5. <input type="button" value="1" onClick="document.getElementById('resultado').value=this.value;">
  6. <input type="button" value="2" onClick="document.getElementById('resultado').value=this.value;">
  7. <input type="button" value="3" onClick="document.getElementById('resultado').value=this.value;">
  8. <input type="button" value="4" onClick="document.getElementById('resultado').value=this.value;">
  9. <input type="button" value="5" onClick="document.getElementById('resultado').value=this.value;">
  10. <input type="button" value="6" onClick="document.getElementById('resultado').value=this.value;">
  11. <input type="button" value="7" onClick="document.getElementById('resultado').value=this.value;">
  12. <input type="button" value="8" onClick="document.getElementById('resultado').value=this.value;">
  13. <input type="button" value="9" onClick="document.getElementById('resultado').value=this.value;">
  14. </div>
  15. <div>
  16. <input class="campotexto" type="text" id="resultado">
  17. </div>
  18. </div>
  19. </body>
  20. </html>
  #3 (permalink)  
Antiguo 27/01/2011, 14:25
 
Fecha de Ingreso: enero-2011
Mensajes: 14
Antigüedad: 13 años, 3 meses
Puntos: 0
Respuesta: Problema insertar numeros

Gracias, pero eso se podia implementar en una funcion en javascript en vez de en el codigo html?
  #4 (permalink)  
Antiguo 27/01/2011, 14:30
 
Fecha de Ingreso: diciembre-2009
Ubicación: Valparaíso
Mensajes: 118
Antigüedad: 14 años, 4 meses
Puntos: 3
Respuesta: Problema insertar numeros

prueba esto...

Código HTML:
Ver original
  1. <script type="text/javascript"">
  2.     function valor(param) {        
  3.         document.getElementById("resultado").value = param.value;    
  4.     }
  5. </head>
  6. <div id="principal">
  7. <div>
  8. <input type="button" value="0" onclick="valor(this);" />
  9. <input type="button" value="1" onclick="valor(this);" />
  10. <input type="button" value="2" onclick="valor(this);" />
  11. <input type="button" value="3" onclick="valor(this);" />
  12. <input type="button" value="4" onclick="valor(this);" />
  13. <input type="button" value="5" onclick="valor(this);" />
  14. <input type="button" value="6" onclick="valor(this);" />
  15. <input type="button" value="7" onclick="valor(this);" />
  16. <input type="button" value="8" onclick="valor(this);" />
  17. <input type="button" value="9" onclick="valor(this);" />
  18. </div>
  19. <div>
  20. <input class="campotexto" type="text" id="resultado" />
  21. </div>
  22. </div>
  23. </body>
  24. </html>
  #5 (permalink)  
Antiguo 27/01/2011, 14:30
Avatar de stramin  
Fecha de Ingreso: marzo-2008
Ubicación: Cubil felino
Mensajes: 1.652
Antigüedad: 16 años, 1 mes
Puntos: 336
Respuesta: Problema insertar numeros

Pero eso es lo que hace, puedes verlo aqui: http://prospektiva.com/forosdelweb/untitled.php

Ahora si quieres que "agregue" el numero presionado al campo de texto tendrias que cambiar esto:

onClick="document.getElementById('resultado').valu e=this.value;"

por esto:

onClick="document.getElementById('resultado').valu e=document.getElementById('resultado').value+this. value;"
  #6 (permalink)  
Antiguo 27/01/2011, 14:35
 
Fecha de Ingreso: enero-2011
Mensajes: 14
Antigüedad: 13 años, 3 meses
Puntos: 0
Respuesta: Problema insertar numeros

Mil gracias a los dos, aunque al poner un numero no me lo "suma" digamos, por ej le doy al 6 y luego le doy otra vez y no pone 66, no se si me explico >_<

Última edición por link_alcala06; 27/01/2011 a las 14:47
  #7 (permalink)  
Antiguo 27/01/2011, 15:26
 
Fecha de Ingreso: enero-2011
Mensajes: 14
Antigüedad: 13 años, 3 meses
Puntos: 0
Respuesta: Problema insertar numeros

Alguien sabe como puedo hacer para concatenar los numeros dentro de la funcion como dije arriba??

Cita:
function pulsar(numero){
document.getElementById("resultado").value=numero. value;
}
  #8 (permalink)  
Antiguo 27/01/2011, 15:31
Avatar de stramin  
Fecha de Ingreso: marzo-2008
Ubicación: Cubil felino
Mensajes: 1.652
Antigüedad: 16 años, 1 mes
Puntos: 336
Respuesta: Problema insertar numeros

prueba así:

Código Javascript:
Ver original
  1. function pulsar(numero){
  2. document.getElementById("resultado").value=document.getElementById("resultado").value+numero.value;
  3. }
  #9 (permalink)  
Antiguo 27/01/2011, 15:41
Avatar de _cronos2
Colaborador
 
Fecha de Ingreso: junio-2010
Mensajes: 2.062
Antigüedad: 13 años, 10 meses
Puntos: 310
Respuesta: Problema insertar numeros

Cita:
Iniciado por stramin Ver Mensaje
prueba así:

Código Javascript:
Ver original
  1. function pulsar(numero){
  2. document.getElementById("resultado").value=document.getElementById("resultado").value+numero.value;
  3. }
Más sencillo:
Código Javascript:
Ver original
  1. document.getElementById("resultado").value+=numero.value;

Saludos (:
__________________
" Getting older’s not been on my plans
but it’s never late, it’s never late enough for me to stay. "
Cigarettes - Russian Red
  #10 (permalink)  
Antiguo 27/01/2011, 15:43
Avatar de stramin  
Fecha de Ingreso: marzo-2008
Ubicación: Cubil felino
Mensajes: 1.652
Antigüedad: 16 años, 1 mes
Puntos: 336
Respuesta: Problema insertar numeros

interesante, no sabia que en javascript se podía hacer eso :D

_cronos2 +karma
  #11 (permalink)  
Antiguo 27/01/2011, 15:56
Avatar de _cronos2
Colaborador
 
Fecha de Ingreso: junio-2010
Mensajes: 2.062
Antigüedad: 13 años, 10 meses
Puntos: 310
Respuesta: Problema insertar numeros

Cita:
Iniciado por stramin Ver Mensaje
interesante, no sabia que en javascript se podía hacer eso :D

_cronos2 +karma
Se puede hacer con otros operadores, y resulta -=, *=, /=, %=, ...
Saludos (:
PD: Gracias por el karma ;)
__________________
" Getting older’s not been on my plans
but it’s never late, it’s never late enough for me to stay. "
Cigarettes - Russian Red
  #12 (permalink)  
Antiguo 27/01/2011, 19:33
 
Fecha de Ingreso: enero-2011
Mensajes: 14
Antigüedad: 13 años, 3 meses
Puntos: 0
Respuesta: Problema insertar numeros

Mil gracias por los dos ejemplos =)
Por cierto estaba haciendo una funcion para limpiar el resultado pero por ahora no doy con ella >_<

Cita:
function borrar(res){
var ce=0;
document.getElementById("resultado").innerHTML=ce;

}
  #13 (permalink)  
Antiguo 28/01/2011, 04:50
 
Fecha de Ingreso: enero-2011
Mensajes: 14
Antigüedad: 13 años, 3 meses
Puntos: 0
Respuesta: Problema insertar numeros

Listo, creo que ya la saque ^^U

Código Javascript:
Ver original
  1. function borrar(res){
  2.     ce=parseInt(0);
  3.     document.getElementById("resultado").value=ce;
  4. }
  #14 (permalink)  
Antiguo 28/01/2011, 08:44
Avatar de stramin  
Fecha de Ingreso: marzo-2008
Ubicación: Cubil felino
Mensajes: 1.652
Antigüedad: 16 años, 1 mes
Puntos: 336
Respuesta: Problema insertar numeros

Si quieres borrarlo asignale una cadena vacía (o si quieres un 0).

document.getElementById("resultado").value="";
  #15 (permalink)  
Antiguo 28/01/2011, 10:16
 
Fecha de Ingreso: enero-2011
Mensajes: 14
Antigüedad: 13 años, 3 meses
Puntos: 0
Respuesta: Problema insertar numeros

Cita:
Iniciado por stramin Ver Mensaje
Si quieres borrarlo asignale una cadena vacía (o si quieres un 0).

document.getElementById("resultado").value="";
¿Null tambien podria valer no?
  #16 (permalink)  
Antiguo 28/01/2011, 11:13
Avatar de stramin  
Fecha de Ingreso: marzo-2008
Ubicación: Cubil felino
Mensajes: 1.652
Antigüedad: 16 años, 1 mes
Puntos: 336
Respuesta: Problema insertar numeros

nunca he intentado darle un valor null a un campo de texto, tendrías que probar.
  #17 (permalink)  
Antiguo 28/01/2011, 12:42
Avatar de _cronos2
Colaborador
 
Fecha de Ingreso: junio-2010
Mensajes: 2.062
Antigüedad: 13 años, 10 meses
Puntos: 310
Respuesta: Problema insertar numeros

Cita:
Iniciado por link_alcala06 Ver Mensaje
¿Null tambien podria valer no?
Sí, en teoría tendrá que funcionar, pero si tienes '', ¿para qué vas a complicarte la vida con null?
Saludos :D
__________________
" Getting older’s not been on my plans
but it’s never late, it’s never late enough for me to stay. "
Cigarettes - Russian Red

Etiquetas: numeros
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 22:45.