如何在此 Jsp 代码中选择多个选项?

发布于 2024-12-09 04:52:08 字数 1298 浏览 0 评论 0原文

<%
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对风讲故事 2024-12-16 04:52:08

HTML 单选按钮只允许进行单一选择。您需要用 HTML 复选框替换它们。

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

但是,您没有在任何地方指定输入值。您需要在 value 属性中指定答案(或其 ID)。

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

最后,您可以通过 HttpServletRequest#getParameterValues() 检索所有检查的值:

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

使用 scriptlet 与具体问题无关(那些 < ;% %> things) 是 90 年代编写 JSP 的方式,是 十年来一直不鼓励。您当前的代码可以更干净地重写为:

<%@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>

我还建议使用更多的自记录变量名称,而不是诸如 qstdDet之类的神秘名称Ans_a。从长远来看,你会给自己和你的继任者带来很多好处。我还建议学习如何将样式分离到独立的 CSS 文件中,以最大限度地减少 HTML 样板文件。

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.

も星光 2024-12-16 04:52:08

您需要添加一个额外的字段来将 userSelection 保存在 TestQuestion 类中,并且必须更新该字段(使用适当的值 - 1,2,3,4 取决于答案序列)当用户提交他/她的选择时。呈现问题和答案时,为选定的 radio html 输入标记嵌入 checked='checked' 属性。

PS:不要使用 scriptlet。始终使用EL/JSTL

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文