You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Bill Clinton <bc...@snipermail.com> on 2001/06/14 22:05:44 UTC

Radio bution Question = How can I change the radio button name?

Hello -
I am writing a page that has a dynamic list of questions that require 
radio button responses. In my actionform, I am storing the list of 
questions in an ArrayList of QuestionAnswer objects. These 
QuestionAnswer objects are basically the id of the question, the text of 
the question, and the answerId.

My jsp code looks like this:


<html:form action="/answerQuestions.do">
<table>
<tr>
<td align=center>Question</td>
<td align=center colspan=3>Answer</td>
</tr>
<logic:iterate id="item" name="questions" scope="request" 
property="questionAnswers" type="com.company.QuestionAnswer" 
property="questionAnswers">
<tr>
<td><bean:write name="item" property="questionDesc"/></td>
<td><html:radio name="item" property="answerId" value="0"/>n/a</td>
<td><html:radio name="item" property="answerId" value="1"/>yes</td>
<td><html:radio name="item" property="answerId" value="2"/>no</td>
</tr>
</logic:iterate>
</table>
<html:submit/>
</html:form>

So, I basically iterate through the list of questions, and it works, for 
the most part. The problem is that the html:radio tag uses the property 
name as the radio button name, and there seems to be no way to override 
this. So the html code ends up looking like this:

<tr>
<td>Question 1:</td>
<td><input type="radio" name="answerId" value="0">no answer</td>
<td><input type="radio" name="answerId" value="1" checked="true">yes</td>
<td><input type="radio" name="answerId" value="2">no</td>
</tr>
<tr>
<td>Question 2:</td>
<td><input type="radio" name="answerId" value="0">no answer</td>
<td><input type="radio" name="answerId" value="1">yes</td>
<td><input type="radio" name="answerId" value="2" checked="true">no</td>
</tr>
<tr>
<td>Question 3:</td>
<td><input type="radio" name="answerId" value="0">no answer</td>
<td><input type="radio" name="answerId" value="1" checked="true">yes</td>
<td><input type="radio" name="answerId" value="2">no</td>
</tr>


So, instead of getting 3 groups of 3 options, I am essentially getting 1 
group of 9 options, because it is using the same name over and over 
again. Is there any way to overide the "name" value for the radio tag? 
It does not seem very flexible to me, is this an oversight that may be 
fixed? Would my only option be to write my own custom tag that extends 
the functionality of struts html:radio tag? Or possibly I should rethink 
the way I am storing the questions in my actionform?

Thanks,
Bill