如何在此 Jsp 代码中选择多个选项?
<%
List qs=(List)request.getAttribute("listOfquestions");
Iterator questions= qs.iterator();
for(int i=1;i<qs.size();i++){
tqDet=(TestQuestion)qs.get(i);
%>
<tr>
<td colspan="2" align="left"><b class="textboldblack"><%out.println(i);%><%out.println(tqDet.getQuestions());%></td></tr>
<tr><td width="22%"align="left"><b class="textboldblack"><input type="radio" name="s"><%out.println(tqDet.getAns_a());%></td></tr>
<tr><td width="22%"align="left"><b class="textboldblack"><input type="radio" name="s"><%out.println(tqDet.getAns_b());%></td></tr>
<tr><td width="22%"align="left"><b class="textboldblack"><input type="radio" name="s"><%out.println(tqDet.getAns_c());%></td></tr>
<tr><td width="22%"align="left"><b class="textboldblack"><input type="radio" name="s"><%out.println(tqDet.getAns_d());%></td></tr>
<%
}
%>
<tr><td colspan="2" align="center"><input type="button" value="Submit"></td>
在上面的 JSP 代码中,我在选择单选按钮时遇到问题。例如,我以列表的形式从数据库中获取 10 个问题。在 JSP 中,我迭代该列表并通过选项一一检索问题,问题是在十个问题中我只能选择一个答案。当我尝试选择另一个答案时,我会丢失之前选择的答案。
<%
List qs=(List)request.getAttribute("listOfquestions");
Iterator questions= qs.iterator();
for(int i=1;i<qs.size();i++){
tqDet=(TestQuestion)qs.get(i);
%>
<tr>
<td colspan="2" align="left"><b class="textboldblack"><%out.println(i);%><%out.println(tqDet.getQuestions());%></td></tr>
<tr><td width="22%"align="left"><b class="textboldblack"><input type="radio" name="s"><%out.println(tqDet.getAns_a());%></td></tr>
<tr><td width="22%"align="left"><b class="textboldblack"><input type="radio" name="s"><%out.println(tqDet.getAns_b());%></td></tr>
<tr><td width="22%"align="left"><b class="textboldblack"><input type="radio" name="s"><%out.println(tqDet.getAns_c());%></td></tr>
<tr><td width="22%"align="left"><b class="textboldblack"><input type="radio" name="s"><%out.println(tqDet.getAns_d());%></td></tr>
<%
}
%>
<tr><td colspan="2" align="center"><input type="button" value="Submit"></td>
In the above JSP code i am getting problem while selecting the radio buttons. For example I am getting 10 questions from database in the form of list. In JSP I am iterating that list and retrieving questions one by one with options, the problem is in ten questions I am able to select only one answer. When I am trying to select another answer I am losing previously selected answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HTML 单选按钮只允许进行单一选择。您需要用 HTML 复选框替换它们。
但是,您没有在任何地方指定输入值。您需要在
value
属性中指定答案(或其 ID)。最后,您可以通过
HttpServletRequest#getParameterValues()
检索所有检查的值:使用 scriptlet 与具体问题无关(那些
< ;% %>
things) 是 90 年代编写 JSP 的方式,是 十年来一直不鼓励。您当前的代码可以更干净地重写为:我还建议使用更多的自记录变量名称,而不是诸如
qs
、tdDet
、之类的神秘名称Ans_a
。从长远来看,你会给自己和你的继任者带来很多好处。我还建议学习如何将样式分离到独立的 CSS 文件中,以最大限度地减少 HTML 样板文件。HTML radio buttons allows only a single selection. You need to replace them by HTML checkboxes.
However, you didn't specify the input value anywhere. You need to specify the answer (or its ID) in
value
attribute.Finally, you can retrieve all checked values by
HttpServletRequest#getParameterValues()
:Unrelated to the concrete problem, using scriptlets (those
<% %>
things) is a 90's way of writing JSPs and is discouraged since a decade. Your current code can more cleanly be rewritten as:I'd also suggest to use more self-documenting variable names instead of something cryptic such as
qs
,tdDet
,Ans_a
. You'd do yourself and your successors a lot of favour in long term. I'd also suggest to learn about separating styles into a standalone CSS file to minimize HTML boilerplate.您需要添加一个额外的字段来将
userSelection
保存在TestQuestion
类中,并且必须更新该字段(使用适当的值 - 1,2,3,4 取决于答案序列)当用户提交他/她的选择时。呈现问题和答案时,为选定的radio
html 输入标记嵌入checked='checked'
属性。PS:
不要使用 scriptlet
。始终使用EL/JSTL
。You need to add an extra field to save
userSelection
inTestQuestion
class and this field must be update (with appropriate value - 1,2,3,4 depends upon the answer sequence) when a user submit his/her selection. While rendering the questions and answers, embedchecked='checked'
attribute for selectedradio
html input tag.PS:
Do not use scriptlets
. Always useEL/JSTL
.