The visitor must answer 15 questions on a test where each question has 3 options, the form has 15 groups of "radio buttons". I managed to get all the answers from the form into an array but I don't know how to obtain the average automatically. Here is the code:
HTML CODE for simplicity of one radio group:
Javascript Code (event handler) just in case you don't get it.
Javascript Code (is everything I have until now):
Thanks for any advice
HTML CODE for simplicity of one radio group:
Code:
<form method="post" action="goTest.php" name="cuestionario" id="form">
<p>1. ¿Haces algún tipo de ejercicio?</p>
<input type="radio" name="pregunta1" value="A" />
<p>A. Nunca</p>
<input type="radio" name="pregunta1" value="B" />
<p>B. A veces</p>
<input type="radio" name="pregunta1" value="C" />
<p>C. Frecuentemente</p>
Javascript Code (event handler) just in case you don't get it.
Code:
function addEvent(elem, evtType, func)
{
if (elem && typeof(elem) == "object")
{
// Primero intentar con métodos DOM
if (elem.addEventListener)
{
elem.addEventListener(evtType, func, false);
}
// de otra forma usar tecnica tradicional
else
{
elem["on" + evtType] = func;
}
}
}
Javascript Code (is everything I have until now):
Code:
// JavaScript Document
addEvent(window, 'load', iniciar);
var opciones; //variable global
function iniciar() {
// iniciar función solo si es un browser moderno que acepte DOM
if (document.getElementById) {
//variable del objeto "form"
oForm=document.getElementById("form");
//variable del boton "ver resultados":
goRespuestas=document.getElementById("resultados");
//si existe...
if (goRespuestas) {
//se consigna evento y su funcion
//Todos los eventos son manejados por el archivo "jsb-globals.js".
addEvent(goRespuestas,'click',respuestas1);
}
}
}
function respuestas1() {
var radios=document.getElementsByTagName("input");
var values;
for (var i=0; i<radios.length; i++) {
if (radios[i].type==="radio" && radios[i].checked) {
values=radios[i].value;
answers=new Array(values);
checkSuma(answers);
}
}
}
function checkSuma(answers) {
for (var i=0; i<answers.length; i++) {
if (answers[i]=="A") {
answers[0]==1;
alert(answers[0]);
} else if (answers[i]=="B") {
answers[1]==2;
alert(answers[1]);
} else if (answers[i]=="C") {
answers[2]==3;
alert(answers[2]);
}
}
}
Thanks for any advice