0

我有一个视图 servlet,它查看数据库中的用户,在每一行查看用户的表,我们有按钮将这一行的单元格更改为文本框。

我面临的问题是,当我获取数据并进行循环以查看每行内的表格时,我打印了一个更新按钮并将其附加到 Javascript 函数。传递此动态行的 id 的重要一点,当我运行代码时没有任何反应;说明有问题!

这是视图 servlet 的代码:

@SuppressWarnings("serial")

public class View extends HttpServlet {

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    resp.setContentType("text/html");

    PrintWriter out = resp.getWriter();
    out.println("<html> <head>");
    out.println("<script type='text/javascript'>");
    

    out.println("function show_textBOX(i){");
    out.println("alert(\"update  these users -- \"+i+ \" -- \")");
    out.println(" document.getElementById(i).innerHTML ='f';");
    out.println("}");

    out.println("</script>");
    out.println(" </head> <body>");
    

    PersistenceManager pm = PMF.get().getPersistenceManager();
    Query query = pm.newQuery("select from " + User.class.getName());

    List<User> list = (List<User>) query.execute();

    resp.getWriter().println("<form method='' action='' name='formMY'>");

    resp.getWriter()
            .println(
                    " <table border = '1'> <caption><h4>all users </h4></caption> ");
    resp.getWriter()
            .println(
                    "</br> <h2> <tr> "
                            + "<td> "
                            + " USERname "

                            + "</td> <td> Salary  "
                            + " </td> <td>BirthDate "
                            + " </td> <td> Modification1 </td> <td> Modification2 </td> </tr></h2>");
    int i = 1;
    for (User obj : list) {
        DateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

        String today = formatter.format(obj.getDate());

        resp.getWriter()
                .println(
                        "</br> <tr id='"
                                + i
                                + "'> <td>"
                                + obj.getName()
                                + " </td> <td> "
                                + obj.getSalary()
                                + "</td> "
                                + "  <td> "
                                + today
                                + "</td>"
                                + " <td>"
                                + " <input type=button value='Update' onclick()='show_textBOX("
                                + i + ")'>" + " </td> <td>"
                                + " <input type=button value='DELETE' >"
                                + " </td>" + "  </tr>");
        i++;
    }

    resp.getWriter().println("</table> </form>");

    resp.getWriter().println("</body> </html> ");
4

1 回答 1

0

你可以使用这个简单的javascript函数

function submitform(obj,action){
    act = document.getElementById("act");
    act.value = action;
    obj.submit();
    }

你会打电话给它

" <td><input type=button value='Update' onclick()='submitform(this.form,"+i+")'></td>"

哪个

this.form

调用相同的表单,然后它将动作参数传递给你的 servlet,你将从中处理它

req.getParameter("action")

注意:我没有测试它,因为我的 JDEVELOPER 不工作:(

于 2012-04-24T16:54:04.547 回答