0

我正在提交带有单选按钮的表单。我希望在提交表单后选中该单选按钮。基本上我在页面上做一些搜索过程。请检查代码。

在我的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 脚本,我没有得到准确的输出。

在此处输入图像描述

4

2 回答 2

1

您的脚本出现在单选按钮之前,并且不包含延迟到稍后的函数。

您正在尝试在按钮存在之前对其进行修改。

将脚本移动到 HTML 源代码中的按钮之后。

于 2013-04-11T10:05:18.707 回答
0

我想建议您,当您的 JSP 页面提交时,您可以使用以下代码在 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"));
if(request.getParameter("radio")!= "" || request.getParameter("radio")!=null){
 request.setAttribute("radio");
}
                RequestDispatcher dispatcher = request.getRequestDispatcher("/radiobutton.jsp");
                System.out.println("--------------"+request.getParameter("radio"));
                dispatcher.forward(request, response);
            } 
            finally 
            {            
                out.close();
            }

        }

现在在您的 JSP 文件中添加以下行:

<input id="rdone" type="radio" name="radio" value="one" <%=((request.getAttribute("radio")!=null && request.getAttribute("radio").toString().equals("one")) ? "checked" : "")%> />
<input id="rdtwo" type="radio" name="radio" value="two" <%=((request.getAttribute("radio")!=null && request.getAttribute("radio").toString().equals("two")) ? "checked" : "")%> />
<input id="rdthree" type="radio" name="radio" value="three" <%=((request.getAttribute("radio")!=null && request.getAttribute("radio").toString().equals("three")) ? "checked" : "")%> />
于 2014-12-14T15:37:19.610 回答