开发者

How can I select more than one option in this Jsp code?

开发者 https://www.devze.com 2023-04-12 08:26 出处:网络
<% List qs=(List)request.getAttribute(\"listOfquestions\"); Iterator questions= qs.iterator(); for(int i=1;i<qs.size();i++){
<%
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 t开发者_JS百科o select another answer I am losing previously selected answer.


HTML radio buttons allows only a single selection. You need to replace them by HTML checkboxes.

<input type="checkbox" name="answer">

However, you didn't specify the input value anywhere. You need to specify the answer (or its ID) in value attribute.

<input type="checkbox" name="answer" value="<%=tdDet.getAns_a()%>">

Finally, you can retrieve all checked values by HttpServletRequest#getParameterValues():

String[] answers = request.getParameterValues("answer");
// ...

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:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jst/core" %>
...
<c:forEach items="${listOfQuestions}" var="question" varStatus="loop">
  <tr><td colspan="2" align="left"><b class="textboldblack">${loop.index + 1}${question.questions}</td></tr>
  <tr><td width="22%" align="left"><b class="textboldblack"><input type="checkbox" name="s" value="${question.ans_a}">${question.ans_a}</td></tr>
  <tr><td width="22%" align="left"><b class="textboldblack"><input type="checkbox" name="s" value="${question.ans_b}">${question.ans_b}</td></tr>
  <tr><td width="22%" align="left"><b class="textboldblack"><input type="checkbox" name="s" value="${question.ans_c}">${question.ans_c}</td></tr>
  <tr><td width="22%" align="left"><b class="textboldblack"><input type="checkbox" name="s" value="${question.ans_d}">${question.ans_d}</td></tr>
</c:forEach>
<tr><td colspan="2" align="center"><input type="button" value="Submit"></td>

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.


You need to add an extra field to save userSelection in TestQuestion 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, embed checked='checked' attribute for selected radio html input tag.

PS: Do not use scriptlets. Always use EL/JSTL.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号