怎么把forEach循环出来的多个radio,提交到后台,并将它存入数据库,急,请大家帮忙,谢谢!
<formaction="/institutions/answerServlet"method="post"><tablewidth="970"height="132"b...
<form action="/institutions/answerServlet" method="post">
<table width="970" height="132" border="0" align="center" cellspacing="0"style="border:1px solid #0099FF" >
<c:forEach items="${paper.topics}" var="topics">
<tr><td width="48"> <input type="radio" name="answerResult" id="a1" value="A" /> A. ${topics.topicOptionA}</td> </tr>
<tr> <td width="48"> <input type="radio" name="answerResult" id="a1" value="B" /> A. ${topics.topicOptionB}</td></tr>
<tr> <td width="48"> <input type="radio" name="answerResult" id="a1" value="C" /> A. ${topics.topicOptionC}</td></tr>
<tr><td width="48"> <input type="radio" name="answerResult" id="a1" value="D" /> A. ${topics.topicOptionD}</td></tr>
</c:forEach>
<tr> <td width="48"> <input type="submit" value="交卷"/></td></tr>
</table></form> 展开
<table width="970" height="132" border="0" align="center" cellspacing="0"style="border:1px solid #0099FF" >
<c:forEach items="${paper.topics}" var="topics">
<tr><td width="48"> <input type="radio" name="answerResult" id="a1" value="A" /> A. ${topics.topicOptionA}</td> </tr>
<tr> <td width="48"> <input type="radio" name="answerResult" id="a1" value="B" /> A. ${topics.topicOptionB}</td></tr>
<tr> <td width="48"> <input type="radio" name="answerResult" id="a1" value="C" /> A. ${topics.topicOptionC}</td></tr>
<tr><td width="48"> <input type="radio" name="answerResult" id="a1" value="D" /> A. ${topics.topicOptionD}</td></tr>
</c:forEach>
<tr> <td width="48"> <input type="submit" value="交卷"/></td></tr>
</table></form> 展开
1个回答
展开全部
你页面写的有问题.
循环出来一整页的radio name都叫answerResult
根据radio的特性.这些radio只能选中一个的.
你后台只能获取到1个answerResult.
这样比较好
<c:forEach items="${paper.topics}" var="topics" varStatus="status">
<tr><td width="48"> <input type="radio" name="answerResult${status.index }" value="A" /> A. ${topics.topicOptionA}</td> </tr>
<tr> <td width="48"> <input type="radio" name="answerResult${status.index }" ivalue="B" /> A. ${topics.topicOptionB}</td></tr>
<tr> <td width="48"> <input type="radio" name="answerResult${status.index }" value="C" /> A. ${topics.topicOptionC}</td></tr>
<tr><td width="48"> <input type="radio" name="answerResult${status.index }" value="D" /> A. ${topics.topicOptionD}</td></tr>
</c:forEach>
后台
//获取所有参数Map
Map map = request.getParameterMap();
//循环Map
Set<String> key = map.keySet();
for (Iterator it = key.iterator(); it.hasNext();) {
String s = (String) it.next();
//以answerResult开头的参数就是结果,第几题可以去判断后面跟的数字
if(s.startWith("answerResult")){
System.out.println("第"+s.replace("answerResult","")+"题的结果是:"+map.get(s).toString());
}
}
循环出来一整页的radio name都叫answerResult
根据radio的特性.这些radio只能选中一个的.
你后台只能获取到1个answerResult.
这样比较好
<c:forEach items="${paper.topics}" var="topics" varStatus="status">
<tr><td width="48"> <input type="radio" name="answerResult${status.index }" value="A" /> A. ${topics.topicOptionA}</td> </tr>
<tr> <td width="48"> <input type="radio" name="answerResult${status.index }" ivalue="B" /> A. ${topics.topicOptionB}</td></tr>
<tr> <td width="48"> <input type="radio" name="answerResult${status.index }" value="C" /> A. ${topics.topicOptionC}</td></tr>
<tr><td width="48"> <input type="radio" name="answerResult${status.index }" value="D" /> A. ${topics.topicOptionD}</td></tr>
</c:forEach>
后台
//获取所有参数Map
Map map = request.getParameterMap();
//循环Map
Set<String> key = map.keySet();
for (Iterator it = key.iterator(); it.hasNext();) {
String s = (String) it.next();
//以answerResult开头的参数就是结果,第几题可以去判断后面跟的数字
if(s.startWith("answerResult")){
System.out.println("第"+s.replace("answerResult","")+"题的结果是:"+map.get(s).toString());
}
}
追问
非常感谢!我的后台原来是通过Servlet插入数据库
Answer answer=new Answer(); Integer uid= new Integer(request.getParameter("uid"));
Integer pid= new Integer(request.getParameter("pid")); Integer tid=new Integer(request.getParameter("tid")); answer.setUsers(users); answer.setTopic(topic); answer.setPaper(paper);answer=abiz.AnswerAdd(answer);用merge保存
现在应该怎么弄
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询