JavaScript - Repetir funcion en varias filas

 
Vista:

Repetir funcion en varias filas

Publicado por Desiree Gonzalez (2 intervenciones) el 12/11/2013 21:37:07
Buenas tardes,

Necesito repetir la function del combo anidado del siguiente codigo en las filas que se van agregando, se que es con un array pero no he logrado hacerlo... alguien que me pueda colaborar, gracias.

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<html>
	<head>
		<script language="javascript" type="text/javascript">
 			var posicionCampo=2;
			function agregarUsuario(){
			nuevaFila = document.getElementById("tablaregistro").insertRow(-1);
			nuevaFila.id=posicionCampo;
			nuevaCelda=nuevaFila.insertCell(-1);
			nuevaCelda.innerHTML="<td> <select name='division["+posicionCampo+"]' id='divi"+posicionCampo+"' onchange='CargaCombo();'> <option value=''> </option> <option value='Division 1'>Division 1</option> <option value='Division 2'>Division 2</option> <option value='Division 3'>Division 3</option>   </select> </td>";
			nuevaCelda=nuevaFila.insertCell(-1);
			nuevaCelda.innerHTML="<td> <select name='oco["+posicionCampo+"]' id='oco"+posicionCampo+"'> <option value=''> </option> </select> </td>";
			nuevaCelda=nuevaFila.insertCell(-1);
 
			nuevaCelda.innerHTML="<td><input type='button' value='Eliminar' onclick='eliminarUsuario(this); totalesgeneral("+posicionCampo+");'> </td>";
			posicionCampo++; }
 
			function eliminarUsuario(obj){ var oTr = obj;
			while(oTr.nodeName.toLowerCase()!='tr'){ oTr=oTr.parentNode; }
			var root = oTr.parentNode; root.removeChild(oTr); posicionCampo--;}
		</script>
	</head>
	<body >
	<table id="tablaregistro">
		<TR>
		<select name="division[1]" id="divi" onchange="CargaCombo();" >
			<option value=""> </option>
			<option value="Division 1">Division 1</option>
			<option value="Division 2">Division 2</option>
			<option value="Division 3">Division 3</option>
		</select>
		</TR>
		<TR>
		<select name="oco[1]" id="oco">
			<option value=""> </option>
		</select>
		<TR>
 
		<script type="text/javascript">
			var arrOpciones =  [
				{
					"padre" : "Division 1",
					"value" : "OCO 148",
					"text" : "OCO 148"
				},
				{
					"padre" : "Division 1",
					"value" : "OCO 150",
					"text" : "OCO 150"
				},
				{
					"padre" : "Division 2",
					"value" : "OCO 151",
					"text" : "OCO 151"
				},
				{
					"padre" : "Division 2",
					"value" : "OCO 167",
					"text" : "OCO 167"
				},
				{
					"padre" : "Division 3",
					"value" : "OCO 145",
					"text" : "OCO 145"
				},
				{
					"padre" : "Division 3",
					"value" : "OCO 159",
					"text" : "OCO 159"
				}
			];
 
			var listHijo = document.getElementById("oco");
 
			function LimpiaSelect()
			{
				while(listHijo.firstChild)
				{
					listHijo.removeChild(listHijo.firstChild);
				}
			}
 
			function CreaOption(session)
			{
				var opcion = document.createElement("option");
				opcion.textContent = session.text;
				opcion.value = session.value;
				return opcion;
			}
 
			function CargaCombo()
			{
				LimpiaSelect();
				var seleccion = document.getElementById("divi");
				if(!seleccion[0].selected)
				{
					for(var i = 0; i < arrOpciones.length; i++)
					{
						var padre = arrOpciones[i].padre;
						var validaSeleccion = padre == seleccion[seleccion.selectedIndex].value;
 
						if(validaSeleccion)
						{
							var opcion = CreaOption(arrOpciones[i]);
							listHijo.appendChild(opcion);
						}
					}
				}
			}
 
			CargaCombo();
			document.getElementById("divi").addEventListener("change", CargaCombo, false);
		</script>
			</table>
 
				<td align="left"><INPUT onclick="agregarUsuario()" value="Agregar Registro" type="button"></td>
	</body>
</html>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder