Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Radio Buttons

Status
Not open for further replies.

arundahar

Programmer
Apr 27, 2001
53
GB
Hi i'm in desperate need of some help:
I have a grid of 7 radio buttons across my page, and 7 radio buttons down my page.

7 radio buttons down the page are my questions, and the 7 across are a ranking scale.

I need to check that for each question:
a) ONE radio button has been checked (ONLY ONE!)
and
b) each question must have a DIFFERENT answer..

E.g Question A can have a rank of 1, but Question B cannot have the same rank 1, it must be different!

I know this must involve creating an Array but am not sure of the code. Can anyone help??!!
 
Well,
I do not understand the part that before each question you need a radio button? It will mean that the user will have to answer to one and only one question!
a) is easy part - if you name the radio buttons with one and the same name, they belong to a radio control array and only one of them can be checked.
<form>
Q1. <input type=&quot;radio&quot; name=&quot;q1&quot; value=&quot;1&quot;> 1 <input type=&quot;radio&quot; name=&quot;q1&quot; value=&quot;2&quot;> 2 <input type=&quot;radio&quot; name=&quot;q1&quot; value=&quot;3&quot;> 3 <input type=&quot;radio&quot; name=&quot;q1&quot; value=&quot;4&quot;> 4 <input type=&quot;radio&quot; name=&quot;q1&quot; value=&quot;5&quot;> 5 <input type=&quot;radio&quot; name=&quot;q1&quot; value=&quot;6&quot;> 6 <input type=&quot;radio&quot; name=&quot;q1&quot; value=&quot;7&quot;> 7<br>
Q2. <input type=&quot;radio&quot; name=&quot;q2&quot; value=&quot;1&quot;> 1 <input type=&quot;radio&quot; name=&quot;q2&quot; value=&quot;2&quot;> 2 <input type=&quot;radio&quot; name=&quot;q2&quot; value=&quot;3&quot;> 3 <input type=&quot;radio&quot; name=&quot;q2&quot; value=&quot;4&quot;> 4 <input type=&quot;radio&quot; name=&quot;q2&quot; value=&quot;5&quot;> 5 <input type=&quot;radio&quot; name=&quot;q2&quot; value=&quot;6&quot;> 6 <input type=&quot;radio&quot; name=&quot;q2&quot; value=&quot;7&quot;> 7<br>
</form>

b) looks like a well-known problem of eight queens (in your case seven) - to choose diffrent answers.
I'll try to look for some solution.
 
Thanks i pretty much new how to do the first bit, but that's cleared it up.
What i meant was that for the 7 questions each one must have only ONE radio button checked and that each question MUST have a different answer.

So if question A has option 6 checked for example, then quesion D cannot have option 6 checked either..

Hope you can help me out some more..

Thanks

Arun
 
hey guyz
arundahar, i think i did it :)
catch!

<html>
<head>
<title>validating radios</title>

<script>
<!--
var RateValues=new Array();
RateValues.length=7;

function validateRadios(){
var flag0=false, flag1=false, flag2=false, flag3=false, flag4=false, flag5=false, flag6=false
var tempobj,formobj=document.forms.myform
for (var ii=0; ii<7; ii++){
eval('tempobj=formobj.q'+ii+'r')
var tmp=eval('flag'+ii)
for (var j=0; j<tempobj.length; j++){//alert('j '+j)

//check for what rate checked
if ( tempobj[j].checked==true){
tmp=true
numb=eval(tempobj[j].name.substr(1,1))//;alert(numb+' '+typeof(numb))
RateValues[numb]=tempobj[j].value
//alert('RateValues['+j+'] '+RateValues[j])
continue
}//check for all rates check:
}if (!tmp){ alert('select all'); return false}
}



//check for matches in rates
var temp1,temp2,num
for (var jj=0; jj<RateValues.length; jj++){
temp1=RateValues[jj]
num=jj
for (var kk=RateValues.length-1; kk>-1; kk--){
temp2=RateValues[kk]
if (temp2==temp1 && kk!=num) {alert('match '+jj+' '+RateValues[jj]+' '+kk+' '+RateValues[kk]); return false}
}
}

return true
}


//-->
</script>
</head>

<body bgcolor=&quot;#FFFFFF&quot;>
<form name=myform onsubmit=&quot;return validateRadios()&quot; method=get>
Q1:<input type=radio name=q0>   Rate:  
1<input type=radio name=q0r value=1>
2<input type=radio name=q0r value=2>
3<input type=radio name=q0r value=3>
4<input type=radio name=q0r value=4>
5<input type=radio name=q0r value=5>
6<input type=radio name=q0r value=6>
7<input type=radio name=q0r value=7><br>

Q2:<input type=radio name=q1>   Rate:  
1<input type=radio name=q1r value=1>
2<input type=radio name=q1r value=2>
3<input type=radio name=q1r value=3>
4<input type=radio name=q1r value=4>
5<input type=radio name=q1r value=5>
6<input type=radio name=q1r value=6>
7<input type=radio name=q1r value=7><br>

Q3:<input type=radio name=q2>   Rate:  
1<input type=radio name=q2r value=1>
2<input type=radio name=q2r value=2>
3<input type=radio name=q2r value=3>
4<input type=radio name=q2r value=4>
5<input type=radio name=q2r value=5>
6<input type=radio name=q2r value=6>
7<input type=radio name=q2r value=7><br>

Q4:<input type=radio name=q3>   Rate:  
1<input type=radio name=q3r value=1>
2<input type=radio name=q3r value=2>
3<input type=radio name=q3r value=3>
4<input type=radio name=q3r value=4>
5<input type=radio name=q3r value=5>
6<input type=radio name=q3r value=6>
7<input type=radio name=q3r value=7><br>

Q5:<input type=radio name=q4>   Rate:  
1<input type=radio name=q4r value=1>
2<input type=radio name=q4r value=2>
3<input type=radio name=q4r value=3>
4<input type=radio name=q4r value=4>
5<input type=radio name=q4r value=5>
6<input type=radio name=q4r value=6>
7<input type=radio name=q4r value=7><br>


Q6:<input type=radio name=q5>   Rate:  
1<input type=radio name=q5r value=1>
2<input type=radio name=q5r value=2>
3<input type=radio name=q5r value=3>
4<input type=radio name=q5r value=4>
5<input type=radio name=q5r value=5>
6<input type=radio name=q5r value=6>
7<input type=radio name=q5r value=7><br>


Q7:<input type=radio name=q6>   Rate:  
1<input type=radio name=q6r value=1>
2<input type=radio name=q6r value=2>
3<input type=radio name=q6r value=3>
4<input type=radio name=q6r value=4>
5<input type=radio name=q6r value=5>
6<input type=radio name=q6r value=6>
7<input type=radio name=q6r value=7><br>

<input type=submit value=submit >
</form>
</body>
</html>

.. woof, cool, huh? X-) regards, vic
 
WOW!! Cheers mate, i'll give this a go and get back to you!
Can't say thank you enough!!
::)
 
It seems to work well mate, thanks!
How would i alter the code if the number of questions AND the number of Question options varied?

Eg That had 7 Questions with 7 options, what about 4 questions with 4 options etc..?

Cheers

Arun
 
here u go:
the begining of ur script:

var RateValues=new Array();
var count=4//amount of rates & questions
// 2 have dfrnt amount of q's & rates - think urself :)
RateValues.length=count;
for (var l=0; l<RateValues.length; l++){ eval('flag'+l+'=false') }

function validateRadios(){
var tempobj,formobj=document.forms.myform
for (var ii=0; ii<count; ii++){
regards, vic
 
Cheers mate! I guess you would work out the actual count by counting through the number of radio buttons by using the document.form and cycling through? This should give you the number of radio buttons on the page? I hope!

Arun
 
so what is ur question again?
what is it u want 2 knowout? how 2 make dfrnt number of questions & rates? regards, vic
 
Well sort of, basically the number of questions and the number of rates will ALWAYS be the same. But the number of questions can vary.
So you'll have 4 questions and therefore 4 ratings (16 radio buttons) or 10 questions and therefore 10 ratings (100 radio buttons) - make sense?

Cheers

Arun
 
and again, what is misunderstanding for ya? everything's cool? or there is smth that u want 2 kno?
as i wrote,
var count=4//amount of rates & questions, set manually

if u want 2 set it automatically, u shuld say it, if i am on the right way, hang on a second, i'll write smth for ya

regards, vic
 
here u go:


var RateValues=new Array();
var count

function setAmount(){
var counter=0
var obj=document.forms.myform.elements
for (var ii=0; ii<obj.length; ii++){
var nn=obj[ii].name
var let=nn.substr(0,1)
if (nn.length==2 && let=='q') counter++
}
count=counter
RateValues.length=count;
for (var l=0; l<RateValues.length; l++){ eval('flag'+l+'=false') }
}

.. the rest code is as it was ..

<body bgcolor=&quot;#FFFFFF&quot; onload=&quot;setAmount()&quot;>

got it? regards, vic
 
Sorry mate, yes it would have to be automatic. I need to dynamically pick up the number of questions that are on the page as it will vary page by page..

Thanks for all the help..

Arun
 
What can i say, you're a star!! |-0

Thanks very much..

Arun
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top