【问题标题】:load value to a textbox dynamically将值动态加载到文本框
【发布时间】:2011-08-20 16:18:57
【问题描述】:

先生,我的 jsp 页面 removeCourse.jsp 中有一个 id 为“courseid”的组合框。当我更改组合框的值时,我希望相应的课程名称将显示在 id 为 cname 的文本框中。为此,我制作了一个 servlet,它使用结果集 rs 返回相应的结果。我写的代码如下 Class.forName("com.mysql.jdbc.Driver").newInstance(); 连接 con=DriverManager.getConnection("jdbc:mysql://localhost/online_exam?"+"user=root&password=pass"); 语句stmt=con.createStatement();

        courseid=request.getParameter("courseid");
        ResultSet rs=stmt.executeQuery("select course_name from course where course_id='"+courseid+"'");
        setCourseName s=new setCourseName();
        s.setCourseName(cname);
        while(rs.next())
        {
            cname=rs.getNString("course_name");
            s.setCourseName(cname);

        }
        //response.sendRedirect("http://localhost:8080/ONLINEEXAMINATION/removeCourse2.jsp");
        request.setAttribute("courseName",cname);
        request.getRequestDispatcher("http://localhost:8080/ONLINEEXAMINATION/removeCourse.jsp").forward(request, response);

然后在 jsp 页面中我编写代码来分配值,如下所示

但是当我从组合框中选择课程 ID 时,它不会在文本框中显示课程名称。你能说问题在哪里吗?在 web.xml 我还写了以下内容 搜索Cname /删除课程

【问题讨论】:

    标签: jsp


    【解决方案1】:

    而不是使用

    request.getRequestDispatcher("http://localhost:8080/ONLINEEXAMINATION/removeCourse.jsp").forward(request, response);

    你必须改变路径

    request.getRequestDispatcher("/removeCourse.jsp").forward(request, response);
    

    您提供的网址有误。

    【讨论】:

    • 我也写了你的答案。但是还是不行
    • 在这种情况下如何处理组合框的 onchange 事件
    • 您只需在文本框中显示名称,而不是转发整个请求是一个坏主意。 \n您可以触发 Ajax 调用和组合框的 onChange 事件,当响应到来时,您可以使用 **document.getElementById(textBoxId).value 在文本框中设置值。
    • 如何在 onchange 事件上触发 ajax,你能举个例子吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多