Ver Mensaje Individual
  #1 (permalink)  
Antiguo 09/02/2013, 13:13
Avatar de freesoftwarrior
freesoftwarrior
 
Fecha de Ingreso: marzo-2006
Mensajes: 362
Antigüedad: 18 años, 1 mes
Puntos: 10
Problema con eventos en la creación de inputs dinámicos

Buenas tardes:
Tengo este código que me permite adicionar campos input de manera dinámica

Cita:
<script type="text/javascript">
icremento =0;
function crear(obj)
{
icremento++;

field = document.getElementById('field');
contenedor = document.createElement('div');
contenedor.id = 'div'+icremento;
field.appendChild(contenedor);

boton = document.createElement('input');
boton.type = 'text';
boton.name = 'text'+'[]';
boton.style.border = "1px solid white";
boton.style.background = "#4F6497";
boton.style.color = "white";
boton.style.font = "x-small Arial";
boton.onfocus = function () {estilo(this.id,1)}
boton.onblur = function () {estilo(this.id,2)}
boton.onchange = function () {formato(this.value,'dec',this.id)}
boton.onkeypress = function () {return acceptNum2(event,this.id)}
contenedor.appendChild(boton);

}
Y para llamar esta función hago esto

Cita:
<fieldset id="field" style="overflow: auto; height:70px; border:0px;"></fieldset>

<input type="button" value="Crear caja de texto" onclick="crear(this)" class="botones">


Sin embargo, las que están en negrita no funcionan. El código de esas 3 funciones es el siguiente

Cita:
function estilo(campo,n)
{
if(n==1)
{document.getElementById(campo).className="texto_n ormal_focus";}
else
{document.getElementById(campo).className="texto_n ormal";}
}

var nav4 = window.Event ? true : false;
function acceptNum2(evt,campo) //Sólo números y SÓLO 1 punto decimal
{
// Punto = 46
// Backspace = 8, Enter = 13, '0' = 48, '9' = 57
var key = nav4 ? evt.which : evt.keyCode;
cadena=document.getElementById(campo).value;
if(cadena.indexOf('.')==-1)
{return (key <= 13 || (key >= 48 && key <= 57) || key == 46);}
else
{return (key <= 13 || (key >= 48 && key <= 57));}

}
Estas 3 funciones funcionan muy bien cuando se trata de inputs clásico como este

Cita:
<input type="text" name="peso" id="peso" size="2" maxlength="5" class="texto_normal"
onFocus="estilo(this.id,1);" onBlur="estilo(this.id,2);" onKeyDown="miTabIndex(event,this.id,5);" tabindex="3"
onKeyPress="return acceptNum2(event,this.id);" onChange="formato(this.value,'dec',this.id)">
Lo que necesito es poder crear varios de estos inputs de manera dinámica y a requerimiento del usuario.

¿Alguien sabe por que con los eventos onfocus, onblur y onkeypress no se ejecutan las funciones?

Un saludo desde Lima, Perú