JSP页面间单选按钮选中值传递失败求修正方案
解决JSP单选按钮选中值传递为null的问题
我看了你的代码,发现几个关键问题导致选中值传不到Results.jsp,下面一步步帮你修正:
问题分析
- 表单嵌套错误:你在
while循环内部重复生成<form>标签,这会导致每个问题都被包裹在独立的表单里。而提交按钮只在最后一个表单中,所以只有最后一个问题的单选按钮值会被提交,前面的都不会被包含在请求里。 - 单选按钮ID重复:同一页面内的HTML元素ID必须唯一,你给每个问题的所有单选按钮都设置了相同的
id="a<%= i %>",这不符合规范,虽然不直接影响提交,但会导致DOM操作问题。 - Results.jsp循环范围不匹配:源页面中
iQuestionID=3,只会生成a1、a2、a3三个单选组,但Results.jsp里循环到i<5(也就是i=1到4),会去获取不存在的a4参数,得到null。
修正后的源JSP代码
<%@ page import="java.sql.*" %> <%@ page import="java.io.*" %> <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <style></style> </head> <body> <% int i=1; String url = "jdbc:mysql://127.0.0.1:3306/Quiz"; String username1="root"; String password1="root"; PreparedStatement st = null; ResultSet results = null; int iQuestionID=3; try { Class.forName("com.mysql.jdbc.Driver"); Connection con= DriverManager.getConnection(url,username1,password1); String sql="SELECT Questions.Question, Questions.option1, Questions.option2,Questions.option3, Questions.option4,Questions.correctanswer FROM Questions order by RAND() limit ?"; st = con.prepareStatement(sql); st.setInt(1,iQuestionID); results = st.executeQuery(); %> <br/> <center> <!-- 将<form>移到循环外面,确保所有控件都在同一个表单内 --> <form name="form1" method="post" action="Results.jsp" > <% while (results.next()) { String question = results.getString(1); String opt1 = results.getString(2); String opt2 = results.getString(3); String opt3 = results.getString(4); String opt4 = results.getString(5); String correctAns = results.getString(6); %> <table border="1" width="500px" bgcolor="white" cellspacing="0" cellpadding="0"> <tr> <td width="100%"> <h2 align="center"><font color="red">Online Quiz Application</font></h2> <b>Select Correct Answer</b> <table border="0" width="500px" cellspacing="2" cellpadding="4"> <tr> <td width="50%"> Question:</td> <td><input type="hidden" name="correctAns<%= i %>" value="<%= correctAns %>" /></td> </tr> <tr> <td><%= question %></td></tr> <tr> <td> 1: <input type="radio" name="a<%= i %>" id="a<%= i %>_1" value="<%= opt1 %>" /></td> <td><%= opt1 %></td></tr> <tr> <td> 2: <input type="radio" name="a<%= i %>" id="a<%= i %>_2" value="<%= opt2 %>" /></td> <td><%= opt2 %></td></tr> <tr> <td> 3: <input type="radio" name="a<%= i %>" id="a<%= i %>_3" value="<%= opt3 %>" /></td> <td><%= opt3 %> </td></tr> <tr> <td> 4: <input type="radio" name="a<%= i %>" id="a<%= i %>_4" value="<%= opt4 %>" /> </td> <td> <%= opt4 %> </td></tr> </table> </td> </tr> </table> <br/> <% i++; } %> <!-- 提交按钮放在表单内,循环外面 --> <INPUT type = "submit" name="addSubmit" value = "Press to Submit"> </form> </center> <% results.close(); con.close(); }catch (Exception ex) { ex.printStackTrace(); }finally { } %> </body> </html>
修正后的Results.jsp代码
<%@ page contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html> <html> <body> <% // 和源页面的iQuestionID保持一致,循环1到3 int totalQuestions = 3; String[] selectedAnswers = new String[totalQuestions + 1]; // 索引从1开始 for (int i=1; i<=totalQuestions; i++) { selectedAnswers[i] = request.getParameter("a"+i); out.println("Question " + i + " selected answer: " + selectedAnswers[i] + "<br/>"); } %> </body> </html>
关键修改说明
- 表单位置调整:把
<form>标签移到while循环之前,提交按钮放在循环之后,确保所有问题的单选按钮和隐藏域都属于同一个表单,这样提交时所有参数都会被发送到Results.jsp。 - 唯一ID设置:给每个单选按钮设置了唯一的ID(比如
a1_1、a1_2),避免ID冲突。 - 循环范围匹配:Results.jsp的循环范围和源页面的
iQuestionID一致,只获取实际存在的参数,避免出现不必要的null。
这样修改后,你应该就能正常获取到每个单选按钮的选中值了。
内容的提问来源于stack exchange,提问作者Anonymous6
相关产品推荐
相关产品推荐

