Foros del Web » Programando para Internet » Javascript »

¿Suma automáticamente de valores de un formulario?

Estas en el tema de ¿Suma automáticamente de valores de un formulario? en el foro de Javascript en Foros del Web. ¡Hola! A ver. Estoy haciendo un test de personalidad, por lo que cada respuesta tiene un propio "value". Me gustaría que, al final del formulario, ...
  #1 (permalink)  
Antiguo 18/04/2011, 04:27
Avatar de WizardDani  
Fecha de Ingreso: noviembre-2008
Mensajes: 25
Antigüedad: 16 años
Puntos: 0
¿Suma automáticamente de valores de un formulario?

¡Hola!

A ver. Estoy haciendo un test de personalidad, por lo que cada respuesta tiene un propio "value". Me gustaría que, al final del formulario, hubiese un botón o algo del estilo que al tocar te dijera la suma total de los valores... No tengo la más remota idea de como hacerlo, si queréis ver el tipo de formulario al que me refiero este es el código:

Código:
<html>
<body>
<h3>¿Cuál es tu personalidad?</h3>
<form action="http://www.quizbox.com/builder/result.aspx?max=30&q=10" method="post" target="foo" onsubmit="window.open('', 'foo', 'width=450,height=300,status=yes,resizable=yes,scr ollbars=yes')">
<ol>
<li>Type your question5 here.</li><br />
<input type="radio" name="ans5" value="2"/>choice1<br />
<input type="radio" name="ans5" value="1"/>choice2<br />
<input type="radio" name="ans5" value="3"/>choice3<br /><br />
<li>Type your question6 here.</li><br />
<input type="radio" name="ans6" value="1"/>choice1<br />
<input type="radio" name="ans6" value="2"/>choice2<br />
<input type="radio" name="ans6" value="3"/>choice3<br /><br />
<li>Type your question7 here.</li><br />
<input type="radio" name="ans7" value="2"/>choice1<br />
<input type="radio" name="ans7" value="1"/>choice2<br />
<input type="radio" name="ans7" value="3"/>choice3<br /><br />
<li>Type your question8 here.</li><br />
<input type="radio" name="ans8" value="3"/>choice1<br />
<input type="radio" name="ans8" value="2"/>choice2<br />
<input type="radio" name="ans8" value="1"/>choice3<br /><br />
<li>Type your question9 here.</li><br />
<input type="radio" name="ans9" value="1"/>choice1<br />
<input type="radio" name="ans9" value="3"/>choice2<br />
<input type="radio" name="ans9" value="2"/>choice3<br /><br />
<li>Type your question10 here.</li><br />
<input type="radio" name="ans10" value="2"/>choice1<br />
<input type="radio" name="ans10" value="1"/>choice2<br />
<input type="radio" name="ans10" value="3"/>choice3<br /><br />
</ol>
<p><input type="submit" value="Resultado" onclick="this.value='Espera porfavor...'" /></p>
</form>
</body>
</html>
Entonces al final habría un botón que si lo tocas te saldría un mensaje de alerta diciéndote los puntos que has obtenido... Por ejemplo "Has obtenido 30 puntos, tu personalidad es x".

He buscado en el FAQ pero no encuentro cómo hacerlo para valores múltiples a la vez...

¡Gracias por adelantado!
  #2 (permalink)  
Antiguo 18/04/2011, 04:53
Avatar de WizardDani  
Fecha de Ingreso: noviembre-2008
Mensajes: 25
Antigüedad: 16 años
Puntos: 0
Respuesta: ¿Suma automáticamente de valores de un formulario?

¡Hola de nuevo!

¡Ya lo he conseguido! Simplemente he copido este código y he cambiado las preguntas y respuestas a mi gusto:

Cita:
<HTML>
<HEAD>
<TITLE>Personality Quiz</TITLE>
<script language="javascript">

var Quest = new Array(10); //this sets up an array for all of the answers that are given

function populate() {
// alert("function populate started");
// this function gives each of the answers 0 points so if someone doesn't answer a question
// the whole thing will continue to work
for(var i=0; i<10; i++) { Quest[i]=0; }
}

function total() {
// alert("function total started");
// this function adds the number of points each answer is worth together
myScore=0;
for (var i=0; i<10; i++) { myScore=myScore+Quest[i]; }
analyzer(myScore);
}
myContents = new Array();
myContents[0] = "Others see you as someone they should handle with care You\'re seen as vain, self centered,"
+ " and who is extremely dominant. Others may admire you, wishing they could be more like you, but don\'t always trust you,"
+ " hesitating to become too deeply involved with you.";
myContents[1] = "Others see you as an exciting, highly volatile, rather impulsive personality; a natural leader,"
+ " who\'s quick to make decisions, though not always the right ones. They see you as bold and adventuresome,"
+ " someone who will try anything once; someone who takes chances and enjoys an adventure."
+ " They enjoy being in your company because of the excitement you radiate.";
myContents[2] = "Others see you as fresh, lively, charming, amuzing, practical, and always interesting;"
+ " someone who\'s constantly in the center of attention, but sufficiently well balanced not to let it go their head."
+ " They also see you as kind, considerate, and understanding; someone who will always cheer them up and help them out.";
myContents[3] = "Others see you as sensible, cautious, careful and practical. They see you as clever, gifted,"
+ " or talented, but modest...Not a person who makes friends too quickly or easily, but someone who\'s extremely loyal"
+ " to friends you do make and who expect the same loyalty in return. Those who really get to know you realize"
+ " it takes a lot to shake your trust in yoru friends, but eqally that it takes you a long time to get over it"
+ " if that trust is ever broken.";
myContents[4] = "Your friends see you as painstaking and fussy. They see you as very cautious, extremely careful,"
+ " a slow and steay plodder. It would really suprise them if you ever did something impulsively or on the spur"
+ " of the moment, expecting you to examine everything carefully from every angle and then, usally against it."
+ " They think this reaction is caused partly by your careful nature.";
myContents[5] = "People think you are shy, nervous, and indecisive, someone who needs looking after,"
+ " who always wants someone else to make the decisions and who does not want to get involved with anyone or anything."
+ " They see you as a worrier who always sees problems that dont exist. SOme people think you are boring."
+ " Only those who know you well know that you are not."

function analyzer (myScore) {
// this function uses the total calculated score to figure out which personality type they are
if (myScore>60) { myContentsPtr = 0; }
else { if (myScore > 50) { myContentsPtr = 1; }
else { if(myScore>40) { myContentsPtr = 2; }
else { if(myScore>30) { myContentsPtr = 3; }
else { if(myScore>20) { myContentsPtr = 4; }
else { myContentsPtr = 5; }
}
}
}
}
myDisplay(myContents[myContentsPtr])
}

function myDisplay(myContents) {
//This function will open a new window and show the results calculated
alert(myContents);
}

function saver(q, points) {
// this function puts the points that each answer is worth into the array
q=q-1;
Quest[q]=points
}

</script>
<style type="text/css">
.style1 {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-weight: bold;
font-size: 24px;
color: #FF0080;
}
.style2 {font-family: Arial, Helvetica, sans-serif}
.style3 {color: #FF0080}
.style5 {color: #00FF00}
.style6 {color: #0080FF}
.style11 {color: #80FFFF}
</style>
</HEAD>

<BODY onLoad="populate()">
<FORM NAME="personalityForm">
<span class="style1">Personality Quiz</span><br>
<br>

<span class="style2">

<span class="style3">1. When do you feel your best?</span><br>
<span class="style6">
<INPUT TYPE="RADIO" NAME="q1" onClick="saver(1,2)">in the morning<br>
<INPUT TYPE="RADIO" NAME="q1" onClick="saver(1,4)">during the afternoon & early evening<br>
<INPUT TYPE="RADIO" NAME="q1" onClick="saver(1,6)">late at night<br>
</span><br>

<span class="style3">2. You usually walk</span><br>
<span class="style6">
<INPUT TYPE="RADIO" NAME="q2" onClick="saver(2,6)" >fairly fast, with long steps<br>
<INPUT TYPE="RADIO" NAME="q2" onClick="saver(2,4)">fairly fast, with short, quick steps<br>
<INPUT TYPE="RADIO" NAME="q2" onClick="saver(2,7)">less fast head up, looking the world in the face<br>
<INPUT TYPE="RADIO" NAME="q2" onClick="saver(2,2)">less fast, head down<br>
<INPUT TYPE="RADIO" NAME="q2" onClick="saver(2,1)">very slowly<br>
</span><br>

<span class="style3">3.When talking to people you</span><br>
<span class="style6">
<INPUT TYPE="RADIO" NAME="q3" onClick="saver(3,4)">stand with your arms folder<br>
<INPUT TYPE="RADIO" NAME="q3" onClick="saver(3,2)">have your hands clasped<br>
<INPUT TYPE="RADIO" NAME="q3" onClick="saver(3,5)">have one or both your hands on your hips<br>
<INPUT TYPE="RADIO" NAME="q3" onClick="saver(3,7)">touch or push the person to whom you are talking<br>
<INPUT TYPE="RADIO" NAME="q3" onClick="saver(3,6)">play with your ear, touch your chin, or smooth your hair<br>
</span><br>

<span class="style3">4. When relaxing, you sit with</span><br>
<span class="style6">
<INPUT TYPE="RADIO" NAME="q4" onClick="saver(4,4)">your knees bent with your legs neatly side by side<br>
<INPUT TYPE="RADIO" NAME="q4" onClick="saver(4,6)">your legs crossed<br>
<INPUT TYPE="RADIO" NAME="q4" onClick="saver(4,2)">your legs stretched out or straight<br>
<INPUT TYPE="RADIO" NAME="q4" onClick="saver(4,1)">one leg curled under you<br>
</span><br>

<span class="style3">5. When something really amuses you, you react with</span><br>
<span class="style6">
<INPUT TYPE="RADIO" NAME="q5" onClick="saver(5,6)">a big, appreciative laugh<br>
<INPUT TYPE="RADIO" NAME="q5" onClick="saver(5,4)">a laugh, but not a loud one<br>
<INPUT TYPE="RADIO" NAME="q5" onClick="saver(5,3)">a quiet chuckle<br>
<INPUT TYPE="RADIO" NAME="q5" onClick="saver(5,5)">a sheepish smile<br>
</span><br>

<span class="style3">6. When you go to a party or social gathering you</span><br>
<span class="style6">
<INPUT TYPE="RADIO" NAME="q6" onClick="saver(6,6)">make a loud entrance so everyone notices you<br>
<INPUT TYPE="RADIO" NAME="q6" onClick="saver(6,4)">make a quiet entrance, looking around for someone you know<br>
<INPUT TYPE="RADIO" NAME="q6" onClick="saver(6,2)">make the quietest entrance, trying to stay unnoticed<br>
</span><br>

<span class="style3"><br>7.You're working very hard, concentrating hard, and you're interrupted. Do you..</span><br>
<span class="style6">
<INPUT TYPE="RADIO" NAME="q7" onClick="saver(7,6)">welcome the break<br>
<INPUT TYPE="RADIO" NAME="q7" onClick="saver(7,2)">feel extremely irritated<br>
<INPUT TYPE="RADIO" NAME="q7" onClick="saver(7,4)">vary between these two extremes<br>
</span><br>

<span class="style3">8. Which of the following colors do you like most?</span><br>
<span class="style6">
<INPUT TYPE="RADIO" NAME="q8" onClick="saver(8,6)" >red or orange<br>
<INPUT TYPE="RADIO" NAME="q8" onClick="saver(8,7)" >black<br>
<INPUT TYPE="RADIO" NAME="q8" onClick="saver(8,5)" >yellow or light blue<br>
<INPUT TYPE="RADIO" NAME="q8" onClick="saver(8,4)" >green<br>
<INPUT TYPE="RADIO" NAME="q8" onClick="saver(8,3)" >dark blue or purple<br>
<INPUT TYPE="RADIO" NAME="q8" onClick="saver(8,2)" >white<br>
<INPUT TYPE="RADIO" NAME="q8" onClick="saver(8,1)" >brown or gray<br>
</span><br>

<span class="style3">9. When you are in bed at night, in those last few moments before going to sleep, you lie</span><br>
<span class="style6">
<INPUT TYPE="RADIO" NAME="q9" onClick="saver(9,7)" >stretched out on your back<br>
<INPUT TYPE="RADIO" NAME="q9" onClick="saver(9,6)" >stretched out face down on your stomach<br>
<INPUT TYPE="RADIO" NAME="q9" onClick="saver(9,4)" >on your side, slightly curled<br>
<INPUT TYPE="RADIO" NAME="q9" onClick="saver(9,2)" >with your head on one arm<br>
<INPUT TYPE="RADIO" NAME="q9" onClick="saver(9,1)" >with your head under the covers<br>
</span><br>

<span class="style3"><br>10. You often dream that you are</span><br>
<span class="style6">
<INPUT TYPE="RADIO" NAME="q10" onClick="saver(10,4)" >falling<br>
<INPUT TYPE="RADIO" NAME="q10" onClick="saver(10,2)" >fighting or struggling<br>
<INPUT TYPE="RADIO" NAME="q10" onClick="saver(10,3)" >searching for something or somebody<br>
<INPUT TYPE="RADIO" NAME="q10" onClick="saver(10,5)" >flying or floating<br>
<INPUT TYPE="RADIO" NAME="q10" onClick="saver(10,6)" >you usually have dreamless sleep<br>
<INPUT TYPE="RADIO" NAME="q10" onClick="saver(10,1)" >your dreams are always pleasant.<br>
</span><br>

<br>
<INPUT TYPE="button" NAME="result" Value="Click here to see your result" onClick="total()">
<span>

</FORM>
</BODY>
</HTML>

Lo he sacado de:
http://www.webdeveloper.com/forum/showthread.php?t=147109

¡Gracias igualmente! Pueden cerrar este tema. :)

Etiquetas: suma, formulario
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 21:32.