Ver Mensaje Individual
  #5 (permalink)  
Antiguo 23/07/2012, 15:03
Avatar de Dradi7
Dradi7
 
Fecha de Ingreso: junio-2008
Ubicación: Peru - Lima
Mensajes: 1.518
Antigüedad: 15 años, 10 meses
Puntos: 220
Respuesta: Concatenar nombres de variables

Como menciona maycolalvarez seria mejor usar array o json aca te dejo ejemplos de ambos que son muy parecidos

para ARRAY
Código Javascript:
Ver original
  1. <html>
  2.     <script language="javascript" type="text/javascript">
  3.        
  4.         var Cliente = function(id, nombres){
  5.             this.id = id;
  6.             this.nombres = nombres;
  7.         }
  8.        
  9.         var Clientes = function(){
  10.             this.clientes = new Array();
  11.         }
  12.        
  13.         Clientes.prototype.add = function(c){
  14.             this.clientes.push(c);
  15.         };
  16.        
  17.         Clientes.prototype.getCliente = function(id){
  18.             for(var x = 0; this.clientes[x]; x++){
  19.                 var c = this.clientes[x];
  20.                 if(c.id === id){
  21.                     return c;
  22.                 }
  23.             }
  24.         };
  25.    
  26.         var clientes = new Clientes();
  27.        
  28.         function agregar(){
  29.             var id = document.getElementById("id").value;
  30.             var nombres = document.getElementById("nombres").value;
  31.             var c = new Cliente(id,nombres);
  32.             clientes.add(c);
  33.             console.log(clientes);
  34.         }
  35.        
  36.         function buscar(){
  37.             var id = prompt("Ingresa ID");
  38.             var c = clientes.getCliente(id);
  39.             console.log(c);
  40.         }
  41.        
  42.     </script>
  43.     <body>
  44.         ID: <input type="text" id="id" />
  45.         Nombres: <input type="text" id="nombres" />
  46.         <input type="button" value="Agregar" onclick="agregar();"/>
  47.         <input type="button" value="Buscar" onclick="buscar();"/>
  48.     </body>
  49. </html>

para JSON
Código Javascript:
Ver original
  1. <html>
  2.     <script language="javascript" type="text/javascript">
  3.        
  4.         var Clientes = {
  5.             Cliente: [],
  6.             get: function(id){
  7.                 for(var x = 0, i = this.Cliente.length; x < i; x++ ){
  8.                     var c = this.Cliente[x];
  9.                     if(c.id === id){
  10.                         return c;
  11.                     }
  12.                 }
  13.             }
  14.         };
  15.        
  16.         function agregar(){
  17.             var _id = document.getElementById("id").value;
  18.             var _nombres = document.getElementById("nombres").value;
  19.             Clientes.Cliente.push({id:_id, Nombre: _nombres});
  20.         }
  21.        
  22.         function buscar(){
  23.             var id = prompt("Ingresa ID");
  24.             var c = Clientes.get(id);
  25.             console.log(c);
  26.         }
  27.        
  28.     </script>
  29.     <body>
  30.         ID: <input type="text" id="id" />
  31.         Nombres: <input type="text" id="nombres" />
  32.         <input type="button" value="Agregar" onclick="agregar();"/>
  33.         <input type="button" value="Buscar" onclick="buscar();"/>
  34.     </body>
  35. </html>
__________________
La clave de todo triunfador es eliminar todas sus excusas y sus limitaciones