我正在提交带有单选按钮的表单。我希望在提交表单后选中该单选按钮。基本上我在页面上做一些搜索过程。请检查代码。
在我的jsp上。
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String buttonvalue = (String) request.getParameter("radio");
%>
<%= buttonvalue %>
<form method="post" action="SearchServlet">
<script type='text/javascript'>
var button = "<%= buttonvalue %>";
if(button == "one")
{
alert("1");
document.getElementById("rdone").checked = true;
document.getElementById("rdtwo").checked = false;
document.getElementById("rdthree").checked = false;
}
else if(button == "two")
{
document.getElementById("rdone").checked = false;
document.getElementById("rdtwo").checked = true;
document.getElementById("rdthree").checked = false;
}
else if(button == "three")
{
alert("3");
document.getElementById("rdone").checked = false;
document.getElementById("rdtwo").checked = false;
document.getElementById("rdthree").checked = true;
alert("2");
}
else
{
alert("4");
}
</script>
<input id="rdone" type="radio" name="radio" value="one"/>
<input id="rdtwo" type="radio" name="radio" value="two"/>
<input id="rdthree" type="radio" name="radio" value="three"/>
<input type="submit" value="Submit"/>
</form>
</body>
</html>
并在 Servlet 中。
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try
{
System.out.println(request.getParameter("radio"));
RequestDispatcher dispatcher = request.getRequestDispatcher("/radiobutton.jsp");
System.out.println("--------------"+request.getParameter("radio"));
dispatcher.forward(request, response);
}
finally
{
out.close();
}
}
一切正常。但我收到 java 脚本错误。请检查以下屏幕截图。由于这个 java 脚本,我没有得到准确的输出。