PDA

View Full Version : Radio Button Question


MoRiA
12-03-2002, 12:31 PM
I am trying to make a quiz for my site but am having trouble with the radio buttons.
I have 10 questions with 4 multiple choice answers each. How would I check which of the 4 radio buttons is selected and set it's value to a variable?

For the set of 4 radio buttons I have this code:
<input type="radio" value="A1" name="Q1">
<input type="radio" value="A2" name="Q1">
<input type="radio" value="A3" name="Q1">
<input type="radio" value="A4" name="Q1">

Each question has a different name (Q1 through to Q10).

If it is not possible to set it's value as a variable then I would like to simply set 1 or 0 as the variable for that question so I can add it all up later in another script.

Thanks to anyone who can help.

jalarie
12-03-2002, 02:47 PM
&nbsp;&nbsp;for&nbsp;(Q=1;&nbsp;Q<=10;&nbsp;Q++)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(N=0;&nbsp;N<4;&nbsp;N++)&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Xeq='if&nbsp;(document.forms[0].Q'+Q+'['+N+'].checked)&nbsp;{&nbsp;Q'+Q+'=document.forms[0].Q'+Q+'['+N+'].value;&nbsp;}';
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;eval(Xeq);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}

glenngv
12-04-2002, 02:45 AM
As much as possible, try to avoid using eval(), especially if there is a more appropriate solution.

This is simpler and easier to implement:

var arrQ = new Array();
for (Q=1; Q<=10; Q++) {
for (N=0; N<4; N++) {
if (document.forms[0].elements['Q'+Q][N].checked) arrQ[Q-1]=document.forms[0].elements['Q'+Q][N].value;
}
}


You can now access the values in the array arrQ.