Can't get value out of Struts nested radio buttons

Hi,

I have a problem with the Struts nested radio buttons. I can't get the value out of the radio buttons.

The code is similar to the following. I don't have the access to the original code right now, but the logic is the same.

<html:html>

<script language="javascript">

function submitForm(){

alert( document.form.option.value );

}

</script >

<body>

<nested:form action="/formAction" name="form">

<nested:radio property="option" value="AAA"/> Some label A

<nested:radio property="option" value="BBB"/> Some label B

<nested:radio property="option" value="CCC"/> Some label C

<nested:submit property="submit" value="Submit" onclick="submitForm();"/>

</nested:form>

</body>

</html:html>

I have defined the "option" property in the form. The problem is that, when I click on the Submit button, the javascript alert always shows "undefined". It does not retrieve the radio button's values such as AAA, BBB or CCC. I do not want to define the radio buttons in an iterative way.

Could anyone help to point out where the problem is? How can I retrieve the value when a radio button is selected? Sorry I couldn't find a solution, because most of the posts are talking about iteration with radio buttons.

Thank you.

[1764 byte] By [thewarrencluba] at [2007-11-26 13:02:04]
# 1
alert( document.form.option[0].value );try this instead..regardsShanu
mshanua at 2007-7-7 17:04:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
Do you mean, I must define the property "option" as a String array? Now I define it as a String. Is this wrong?
thewarrencluba at 2007-7-7 17:04:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
When u get the html page see what html tags are inserted for your option..send it across..shanu
mshanua at 2007-7-7 17:04:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

Sorry. Can you explain it clearly?

html generated is standard:

<input type="radio" name="option" value="AAA" checked="checked"/>

<input type="radio" name="option" value="BBB" />

<input type="radio" name="option" value="CCC"/>

I don't see anything special here. Or is there?

Message was edited by:

thewarrenclub

thewarrencluba at 2007-7-7 17:04:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5
Leave javascript error..u have getter method for "option" i guess..From action class c that u are able to retreive the value...if not let me know.regardsshanu
mshanua at 2007-7-7 17:04:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Note that this function is javascript, not java.

My guess is that you have the error at "form" you should be using document.forms[0] or document.[FORM_NAME] where [FORM_NAME] is the name of the form on the page.

The best way I have found to debug javascript expressions is to test/build them up piece by piece until it breaks.

You can also use the address bar to enter in javascript:

alert(document);

alert(document.forms[0]);

alert(document.forms[0].option);

You won't be able to avoid iteration on the javascript side of things. Thats the way the browser does it. thats the way you have to access it in javascript.

evnafetsa at 2007-7-7 17:04:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7
Thanks for your patience, guys. :)I managed to output the value of "option" in the Action class, as mshanu suggested, and the value shows the correct one I selected. So it is the problem with the javascript, not the jsp code. <rant> I hate javascript </rant>
thewarrencluba at 2007-7-7 17:04:34 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...