
Evento onchange en Servlet y JSP con javascript
Publicado por Wasp (24 intervenciones) el 05/08/2016 10:08:57
Buenas a todos/as.
Llevo unos días dándoles vueltas a un tema y a ver si alguien me puede guiar un poco.
Actualmente trabajando mediante un Servet quiero hacer una función en javascript.
Dentro de un método propio para separar partes de código, me creo esto:
Lo que hago es que cuando creo el evento onchange, lo que le paso a la funcion crearNuevoText eso no me trae el objeto que creo, por ejemplo, debería de pasar un String que ponga: "Frecuencia" o "Tolerancia".
en el siguiente método le paso a crearNuevoText ese valor que tiene que coger el onchange, pero no se pasa un valor sino una cadena que es valor y por tanto en el debugger de Chrome me da fallo.
Esto mismo cuando lo hago por jsp y javascript me funciona bien, pero al usar Servlet no hay manera de hacerlo.
¿Alguien me podría decir como pasar ese valor al evento onchange en el árbol DOM?
Si no me he explicado bien, comentármelo y lo haré mas explicativo.
Muchas Gracias!!!!!
Llevo unos días dándoles vueltas a un tema y a ver si alguien me puede guiar un poco.
Actualmente trabajando mediante un Servet quiero hacer una función en javascript.
Dentro de un método propio para separar partes de código, me creo esto:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
public static String pintarJsFuncionalidadFiltroDerechaNumerico(){
String html ;
/** Aqui tenemos el segundo filtro con Numerico */
html = "function obtenerFiltroParametrosNumericos(valor){";
html = html + "var saltoLinea = document.createElement('br');";
html = html + "document.getElementById('divIzquierda').appendChild(saltoLinea);";
//creamos el type checkbox
html = html + "var checkbox = document.createElement('input');";
html = html + "checkbox.setAttribute('type', 'checkbox');";
html = html + "checkbox.setAttribute('id', 'checkbox'+valor.value);";
html = html + "checkbox.setAttribute('name', 'checkbox'+valor.value);";
html = html + "checkbox.setAttribute('checked', 'checked');";
html = html + "document.getElementById('divIzquierda').appendChild(checkbox);";
//creamos la palabra RecursoGlobal que ira a la derecha del checkbox
html = html + "var nombre = document.createTextNode(valor.value+' ');";
html = html + "document.getElementById('divIzquierda').appendChild(nombre);";
//creamos el select
html = html + "var array = ['=', '>=', '<=', '><'];";
html = html + "var contador, option;";
html = html + "var select = document.createElement('select');";
html = html + "select.setAttribute('id', 'select'+valor.value+'ID');";
html = html + "select.setAttribute('name', 'select'+valor.value+'ID');";
//hay que tener cuidado con la concatenacion del crearNuevoText
html = html + "select.setAttribute('onchange', 'crearNuevoText(valor.value)', 'select'+valor.value+'ID');";
html = html + "for(contador = 0; contador < array.length; contador++){";
html = html + "option = document.createElement('option');";
html = html + "option.value = array[contador];";
html = html + "option.text = array[contador];";
html = html + "select.appendChild(option);";
html = html + "}";
html = html + "document.getElementById('divIzquierda').appendChild(select);";
//creamos el type text
html = html + "var text = document.createElement('input');";
html = html + "text.setAttribute('type', 'text');";
html = html + "text.setAttribute('id', 'texto'+valor.value);";
html = html + "text.setAttribute('name', 'texto'+valor.value);";
html = html + "text.setAttribute('size', '4');";
html = html + "document.getElementById('divIzquierda').appendChild(text);";
html = html + "}";
return html;
}
Lo que hago es que cuando creo el evento onchange, lo que le paso a la funcion crearNuevoText eso no me trae el objeto que creo, por ejemplo, debería de pasar un String que ponga: "Frecuencia" o "Tolerancia".
en el siguiente método le paso a crearNuevoText ese valor que tiene que coger el onchange, pero no se pasa un valor sino una cadena que es valor y por tanto en el debugger de Chrome me da fallo.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public static String pintarJsFuncionalidadNuevoTextFiltrosNumericos(){
String html;
html = "function crearNuevoText(valor){";
html = html + "alert(valor);";
html = html + "var seleccionado = document.getElementById('select'+valor+'ID');";
html = html + "var unaVez = document.getElementById('valor');";
html = html + "if((seleccionado.options[seleccionado.selectedIndex].text === '><') && (unaVez.value == 1)){";
html = html + " var text = document.createElement('input');";
html = html + " text.setAttribute('type', 'text');";
html = html + " text.setAttribute('id', 'texto'+valor+'2');";
html = html + " text.setAttribute('name', 'texto'+valor+'2');";
html = html + " text.setAttribute('size', '4');";
html = html + " document.getElementById('divIzquierda').appendChild(text);";
html = html + " unaVez.value = 0;";
html = html + "}";
//creamos el salto de linea
html = html + "var saltoLinea = document.createElement('br');";
html = html + "document.getElementById('divIzquierda').appendChild(saltoLinea);";
html = html + "}";
return html;
}
Esto mismo cuando lo hago por jsp y javascript me funciona bien, pero al usar Servlet no hay manera de hacerlo.
¿Alguien me podría decir como pasar ese valor al evento onchange en el árbol DOM?
Si no me he explicado bien, comentármelo y lo haré mas explicativo.
Muchas Gracias!!!!!
Valora esta pregunta


0