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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking multiple radio groups values and putting result into a textbox 1

Status
Not open for further replies.

kiwieur

Technical User
Apr 25, 2006
200
GB
Hi,
I have tried to write some code which will check the value of multiple radio groups and if any of the vales equal "Fail" then it will put "Fail" into a textbox otherwise it will put "Pass", however it does not seem to work.

Could someone tell me where I am going wrong please

Code:
<script type="text/javascript">
		function pfStatus() {    
			var r1 = document.forms["frmTK"].radChop;
			var r2 = document.forms["frmTK"].radSlit;
			var r3 = document.forms["frmTK"].radScore;
			var r4 = document.forms["frmTK"].radQB;
			var r5 = document.forms["frmTK"].radWarp;
			var r6 = document.forms["frmTK"].radFlute;
			
			var v1 = getSelectedRadio(r1);
			var v2 = getSelectedRadio(r2);
			var v3 = getSelectedRadio(r3);
			var v4 = getSelectedRadio(r4);
			var v5 = getSelectedRadio(r5);
			var v6 = getSelectedRadio(r6);
			
			
			var z1 = r1[v1].value;
			var z2 = r2[v2].value;
			var z3 = r3[v3].value;
			var z4 = r1[v4].value;
			var z5 = r2[v5].value;
			var z6 = r3[v6].value;
						
			if ((z1 == "Fail") || (z2 == "Fail") || (z3 == "Fail") || (z4 == "Fail") || (z5 == "Fail") || (z6 == "Fail")) {
			document.frmTK.txtFailure.value = "Fail";
			} else {
			document.frmTK.txtFailure.value = "Pass";
			}
		}
    </script>
Then on my page I have
Code:
onClick="pfStatus()"
[ponder]

Regards

Paul

 
You could use JQuery...

Code:
<script type="text/javascript">
function checkRadio() {
    
    var ok = "Pass";
    
    $('input:radio:checked').each(function(){
        if($(this).val() == "Fail"){ok = "Fail";}            
    });
    
    $('#txtFailure').val(ok);
}
</script>

Tested using this HTML
Code:
<form action="#" name="form1" id="form1" method="post">
        Result :  <input name="txtFailure" id="txtFailure" type="text" value="">
        <p>radChop<br />
            Pass : <input name="radChop" type="radio" value="Pass" /> Fail : <input name="radChop" type="radio" value="Fail" />
        </p>
        <p>radSlit<br />
            Pass : <input name="radSlit" type="radio" value="Pass" /> Fail : <input name="radSlit" type="radio" value="Fail" />
        </p>
        <p>radScore<br />
            Pass : <input name="radScore" type="radio" value="Pass" /> Fail : <input name="radScore" type="radio" value="Fail" /></td>            
        </p>
        <input type="button" name="Test" value="Test Values" onclick="checkRadio();">
</form>

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
Hi 1DMF,
Thank you for responding, sorry but I have never used JQuery is there something I have to do to use it

Regards

Paul

 
yes, download the JQuery minified JS file and then add the following to the header of your page..
Code:
<script src="jquery-x.x.x.min.js" type="text/javascript"></script>

remember to replace the x.x.x with the correct version number

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
Hi 1DMF,
I Tried your test page but is does not work for me, i must be doing something wrong

Code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="jquery-1.9.1.min.js" type="text/javascript"></script>
</head>

<body>
<script type="text/javascript">
function checkRadio() {
    
    var ok = "Pass";
    
    $('input:radio:checked').each(function(){
        if($(this).val() == "Fail"){ok = "Fail";}            
    });
    
    $('#txtFailure').val(ok);
}
</script> 
<form action="#" name="form1" id="form1" method="post">
        Result :  <input name="txtFailure" id="txtFailure" type="text" value="">
        <p>radChop<br />
            Pass : <input name="radChop" type="radio" value="Pass" /> Fail : <input name="radChop" type="radio" value="Fail" />
        </p>
        <p>radSlit<br />
            Pass : <input name="radSlit" type="radio" value="Pass" /> Fail : <input name="radSlit" type="radio" value="Fail" />
        </p>
        <p>radScore<br />
            Pass : <input name="radScore" type="radio" value="Pass" /> Fail : <input name="radScore" type="radio" value="Fail" /></td>            
        </p>
        <input type="button" name="Test" value="Test Values" onclick="checkRadio();">
</form> 
</body>
</html>

Regards

Paul
 
what error are you getting?

Also remove the erroneous </td> tag..

Code:
<p>radScore<br />
            Pass : <input name="radScore" type="radio" value="Pass" /> Fail : <input name="radScore" type="radio" value="Fail" />[b]</td>[/b]            
        </p>

"In complete darkness we are all the same, it is only our knowledge and wisdom that separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Free Dance Music Downloads
 
I am not getting any value in the textbox, I have removed the td tag

Regards

Paul

 
Hi 1DMF,
Ignore me I had the test page in a sub folder therefore it could not find

Code:
<script src="jquery-1.9.1.min.js" type="text/javascript"></script>
I changed it to this
Code:
<script src="../jquery-1.9.1.min.js" type="text/javascript"></script>
and now it works

Regards

Paul

 
[medal]1DMF,

Thanks for your help, a star for you. [medal][2thumbsup]

Regards

Paul

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top