![]() |
|
|
#1
|
|||
|
|||
|
radio buttons if then else
hi after having been giving great help the othe rday i have one more problem i would like to ask about. im trying to use a function to work out which radio button has been selected. I dont really fully understand functions so i hope i have set this out right . Anyway its not working and errors appear on the page.
am i close? Code:
<form name="payment">
<div align="center">
<table border="0" height="74" width="371">
<tr>
<td height="12" width="600">
<p align="right"><font size="2">I'm buying a £0.50 photo
(1600 x 1200)</font></p>
</td>
<center>
<td height="12" width="26"><input type="radio" value="V1" checked name="R1"></td>
<td height="12" width="29"> </td>
</tr>
</center>
<tr>
<td height="25" width="600">
<p align="right"><font size="2">I'm buying a £1.00 photo (2048
x 1536)</font></p>
</td>
<center>
<td height="25" width="26"><input type="radio" name="R1" value="V2"></td>
<td height="25" width="29"><font size="2">
<img border="0" src="x-click-but23.gif" align="center" width="68" height="23" onclick="decision(<V1>,<V2>,<V3>)">
</font></td>
</tr>
</center>
<tr>
<td height="25" width="600">
<p align="right"><font size="2">I'm buying a Image CD costing
£6.00</font></p>
</td>
<center>
<td height="25" width="26"><input type="radio" name="R1" value="V3"></td>
<td height="25" width="29"></td>
</tr>
</table>
</center>
</div>
<p align="center"><font size="2">
</font>
</p>
<script type="text/javascript">
<!--
function decision(V1,V2,V3){
if(document.form.V1.checked) {
window.alert("test");
} else if(document.form.V2.checked) {
window.alert("test");
} else if(document.form.V3.checked) {
window.alert("test");
} else {
window.alert("Pick something!!");
}
}// -->
</script>
</form>
Last edited by fraser5002; Mar 29th 2005 at 6:09 am. |
|
#2
|
|||
|
|||
|
When radio buttons have the same name to group them together javascript treats them as an array not individual elements. So your function needs to change to:
function decision(V1,V2,V3){ if(document.form.R1[0].checked) { window.alert("test"); } else if(document.form.R1[1].checked) { window.alert("test"); } else if(document.form.R1[2].checked) { window.alert("test"); } else { window.alert("Pick something!!"); } } The parameters you are passing to the function are not used. |
|
#3
|
|||
|
|||
|
thansk for your advice i have changed the code to what you said but i am still getting errors. This is the page
http://homepage.ntlworld.com/fraser_d/buy-scottish-photos.htm i just cannot work it out any ideas of what else im doing wrong? thanks |
|
#4
|
|||
|
|||
|
This line is incorrect:
<img border="0" src="x-click-but23.gif" align="center" width="68" height="23" onclick="decision(<V1>,<V2>,<V3>"> The closing bracket on the decision function call is missing. I also do not see what the <V1> etc parameters are. Change the above line to: <img border="0" src="x-click-but23.gif" align="center" width="68" height="23" onclick="decision()"> and change thew function header to: function decision() { That will remove your javascript errors. |
|
#5
|
|||
|
|||
|
thansk for your advice spot on! nearly there
just a couple more bugs
|
![]() |
| Bookmarks |
| Thread Tools | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Voting Buttons | H-Man | 3 | Sep 25th 2006 9:25 pm | |
| Need person to make some buttons | bluemouse2 | Link Exchange | 10 | Mar 23rd 2005 9:15 am |
| Google Toolbar voting buttons | dfsweb | 10 | Jan 18th 2005 12:46 pm | |