Foros del Web » Programando para Internet » Javascript »

Cotrol sobre radiobuttons

Estas en el tema de Cotrol sobre radiobuttons en el foro de Javascript en Foros del Web. Hola que tal! Quisiera pedirles ayuda para el manejo de radiobuttons con javascript, tengo un formulario en el cual aparecen 4 preguntas y cuando se ...
  #1 (permalink)  
Antiguo 18/01/2005, 15:54
 
Fecha de Ingreso: enero-2005
Mensajes: 1
Antigüedad: 19 años, 3 meses
Puntos: 0
Exclamación Cotrol sobre radiobuttons

Hola que tal!

Quisiera pedirles ayuda para el manejo de radiobuttons con javascript, tengo un formulario en el cual aparecen 4 preguntas y cuando se da click en un boton aparecen las respuestas que contestaron y las respuestas correctas, mi problema esta en que cuando no se contesta una pregunta mi codigo ya no funciona porque no sé como determinar si no se contesto a alguna pregunta. Tal vez no me estoy explicando bien, pero envío mi código a ver si pudieran ayudarme porfavor, se los agradecere mucho, de antemano muchas gracias....... Norma .

<HTML>
<head>
<title>Ejemplo de radiobuttons</title>
</head>

<BODY>
<SCRIPT language="JavaScript">
function muestraResp()
{
var tures1,tures2,tures3,tures4;
for (tures1=0;tures1<activ4.preg1.length;tures1++){
if (activ4.preg1[tures1].checked)
break;
}
for (tures2=0;tures2<activ4.preg2.length;tures2++){
if (activ4.preg2[tures2].checked)
break;
}
for (tures3=0;tures3<activ4.preg3.length;tures3++){
if (activ4.preg3[tures3].checked)
break;
}
for (tures4=0;tures4<activ4.preg4.length;tures4++){
if (activ4.preg4[tures4].checked)
break;
}

activ4.turesp1.value = activ4.preg1[tures1].value;
activ4.turesp2.value = activ4.preg2[tures2].value;
activ4.turesp3.value = activ4.preg3[tures3].value;
activ4.turesp4.value = activ4.preg4[tures4].value;

activ4.respcorr1.value = "1,000,000,000 de kilómetros cúbicos";
activ4.respcorr2.value = "95%";
activ4.respcorr3.value = "0.01%";
activ4.respcorr4.value = "0.0001%";

}

</SCRIPT>
<FORM NAME="activ4">
<p>1. &iquest;Cu&aacute;l es el volumen total de agua en la Tierra?</p>
<input type="radio" name="preg1" value="1,000 kil&oacute;metros c&uacute;bicos">
a) 1,000 kil&oacute;metros c&uacute;bicos
<input type="radio" name="preg1" value="1,000,000 de kil&oacute;metros c&uacute;bicos">
b) 1,000,000 de kil&oacute;metros c&uacute;bicos
<input type="radio" name="preg1" value="1,000,000,000 de kil&oacute;metros c&uacute;bicos">
c) 1,000,000,000 de kil&oacute;metros c&uacute;bicos

<p>2. &iquest;Qu&eacute; porcentaje del agua de la Tierra se encuentra en los
oc&eacute;anos?</p>
<input type="radio" name="preg2" value="25%">a) 25%
<input type="radio" name="preg2" value="50%">b) 50%
<input type="radio" name="preg2" value="95%">c) 95%

<p>3. &iquest;Qu&eacute; porcentaje del agua de la Tierra se encuentra en los
lagos?</p>
<input type="radio" name="preg3" value="10%">a) 10%
<input type="radio" name="preg3" value="25%">b) 25%
<input type="radio" name="preg3" value="0.01%">c) 0.01%

<p>4. &iquest;Qu&eacute; porcentaje del agua de la Tierra se encuentra en los
r&iacute;os?</p>
<p>
<input type="radio" name="preg4" value="10%">a) 10%
<input type="radio" name="preg4" value="0.0001%">b) 0.0001%
<input type="radio" name="preg4" value="0.01%">c) 0.01%

<p>
<input type="button" name="respuestas" value="Respuestas" onClick="muestraResp()">
</p>
<table width="93%" border="1" cellspacing="0" cellpadding="0">
<tr>
<th width="57%">Pregunta</th>
<th width="23%">Tu respuesta</th>
<th width="20%">Respuesta Correcta</th>
</tr>
<tr>
<td width="57%" height="16">1. &iquest;Cu&aacute;l es el volumen total de
agua en la Tierra?</td>
<td width="23%" height="16">
<input type="text" readonly name="turesp1" size="35" maxlength="35">
</td>
<td width="20%" height="16">
<input type="text" readonly name="respcorr1" size="35" maxlength="35">
</td>
</tr>
<tr>
<td width="57%">2. &iquest;Qu&eacute; porcentaje del agua de la Tierra se
encuentra en los oc&eacute;anos?</td>
<td width="23%">
<input type="text" readonly name="turesp2" size="10" maxlength="10">
</td>
<td width="20%">
<input type="text" readonly name="respcorr2" size="10" maxlength="10">
</td>
</tr>
<tr>
<td width="57%">3. &iquest;Qu&eacute; porcentaje del agua de la Tierra se
encuentra en los lagos?</td>
<td width="23%">
<input type="text" readonly name="turesp3" size="10" maxlength="10">
</td>
<td width="20%">
<input type="text" readonly name="respcorr3" size="10" maxlength="10">
</td>
</tr>
<tr>
<td width="57%">4. &iquest;Qu&eacute; porcentaje del agua de la Tierra se
encuentra en los r&iacute;os?</td>
<td width="23%">
<input type="text" readonly name="turesp4" size="10" maxlength="10">
</td>
<td width="20%">
<input type="text" readonly name="respcorr4" size="10" maxlength="10">
</td>
</tr>
</table>
</FORM>
</body>
</HTML>
  #2 (permalink)  
Antiguo 19/01/2005, 02:19
 
Fecha de Ingreso: mayo-2004
Mensajes: 75
Antigüedad: 19 años, 11 meses
Puntos: 2
Para ver si han contestado a una pregunta basta con fijarse en si el indice con el que recorres esa pregunta (tures1, tures2 ...) ha llegado hasta el final (el numero de respuestas totales, vamos el .length), si es asi es que no han contestado porque si hubiesen contestado el indice sería menor.

Código:
function muestraResp()
{
var tures1,tures2,tures3,tures4; 
for (tures1=0;tures1<activ4.preg1.length;tures1++){ 
if (activ4.preg1[tures1].checked) 
break; 
} 
for (tures2=0;tures2<activ4.preg2.length;tures2++){ 
if (activ4.preg2[tures2].checked) 
break; 
} 
for (tures3=0;tures3<activ4.preg3.length;tures3++){ 
if (activ4.preg3[tures3].checked) 
break; 
} 
for (tures4=0;tures4<activ4.preg4.length;tures4++){ 
if (activ4.preg4[tures4].checked) 
break; 
}

//si no han contestado a la pregunta el indice 
//es el mismo que el numero de opciones totales
//que tiene la pregunta
if(tures1 == activ4.preg1.length)
	//aqui pones el valor que quieras mostrar como respuesta si
	//no han contestado la pregunta
	activ4.turesp1.value = "";
else	
	activ4.turesp1.value = activ4.preg1[tures1].value;
if(tures2 == activ4.preg2.length)
	activ4.turesp2.value = "";
else
	activ4.turesp2.value = activ4.preg2[tures2].value;
if(tures3 == activ4.preg3.length)
	activ4.turesp3.value = "";
else
	activ4.turesp3.value = activ4.preg3[tures3].value;
if(tures4 == activ4.preg4.length)
	activ4.turesp4.value = "";
else
	activ4.turesp4.value = activ4.preg4[tures4].value;

//yo no veo bien estos acentos, para verlos yo lo pongo asi
//activ4.respcorr1.value = activ4.preg1[2].value;
activ4.respcorr1.value = "1,000,000,000 de kilómetros cúbicos";
activ4.respcorr2.value = "95%";
activ4.respcorr3.value = "0.01%";
activ4.respcorr4.value = "0.0001%";
}
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 09:31.