【问题标题】:Table generated from DB using sevlet not getting submitted when form is submitted提交表单时使用 sevlet 从 DB 生成的表未提交
【发布时间】:2017-04-28 08:17:58
【问题描述】:

我正在根据来自 DB 的输入创建一个表,并将其显示在我的 div 中,如下所示:

小服务程序:

out.print("<table id=prev class=display nowrap stripe row-border order-column>");
   out.print("<thead>");
   out.print("<tr>" +
"<th>Form Id</th>\n" +
"                <th>Form Name</th>\n" +
"                <th>Open</th>\n" +
"                <th>Report</th>\n" +
"                <th>View</th>\n" +
"                <th>Insert</th>\n" +
"                <th>Update</th>\n" +
"                <th>Delete</th>\n" +
"                <th>Approve Transaction</th>\n" +
"                <th>Reject Transaction</th>\n" +
"                   </tr>");
   out.print("</thead>");
    out.print("<tfoot>");
   out.print("<tr>" +
"<th>Form Id</th>\n" +
"                <th>Form Name</th>\n" +
"                <th>Open</th>\n" +
"                <th>Report</th>\n" +
"                <th>View</th>\n" +
"                <th>Insert</th>\n" +
"                <th>Update</th>\n" +
"                <th>Delete</th>\n" +
"                <th>Approve Transaction</th>\n" +
"                <th>Reject Transaction</th>\n" +
"                   </tr>");
   out.print("</tfoot>");
   out.println("<tbody>");
 while(rs1.next())
    {out.print("<tr><td name=prvlg>"+rs1.getInt("FORM_ID")+"</td>"
           + "<td name=prvlg>"+rs1.getString("FORM_NAME")+"</td>");
   if(form1==1){ out.print("<td name=prvlg><input type=checkbox checked=checked></td>");}
   else { out.print("<td name=prvlg><input type=checkbox></td>");}
   if(report==1)
       { out.print("<td name=prvlg><input type=checkbox checked=checked></td>");}
   else { out.print("<td name=prvlg><input type=checkbox></td>");}
   if(view==1)
       { out.print("<td name=prvlg><input type=checkbox checked=checked></td>");}
   else { out.print("<td name=prvlg><input type=checkbox></td>");}
    if(insert1==1)
       { out.print("<td name=prvlg><input type=checkbox checked=checked></td>");}
   else { out.print("<td name=prvlg><input type=checkbox></td>");}
     if(update==1)
       { out.print("<td name=prvlg><input type=checkbox checked=checked></td>");}
   else { out.print("<td name=prvlg><input type=checkbox></td>");}
      if(delete==1)
       { out.print("<td name=prvlg><input type=checkbox checked=checked></td>");}
   else { out.print("<td name=prvlg><input type=checkbox></td>");}
       if(isapproved==1)
       { out.print("<td name=prvlg><input type=checkbox checked=checked></td>");}
   else { out.print("<td name=prvlg><input type=checkbox></td>");}
       if(not_approved==1)
       { out.print("<td name=prvlg><input type=checkbox checked=checked></td>");}
   else { out.print("<td name=prvlg><input type=checkbox></td>");}

   }
   out.print("</tbody></table>");
}

并在此处显示:

 if ((zreq.readyState == 4) && (zreq.status == 200)) {

                            document.getElementById("user_prev").innerHTML = zreq.responseText;
                           $("#parent").DataTable({
                               dom: 't',
                           });

并且 div 在表单内。提交时,所有其他信息都被提交,但表格内容除外。

String td[]=request.getParameterValues("prvlg");

它始终为空。谁能建议我哪里出错了?

【问题讨论】:

    标签: javascript jquery forms jsp servlets


    【解决方案1】:

    表单字段(input、textarea、select、..)应该有一个name 属性,以便提交。

    类似的行:

    <input type=checkbox checked=checked>
    

    应该看起来像:

    <input name=parameterName type=checkbox checked=checked>
    

    然后在服务器上你可以访问这个参数:

    request.getParameter("parameterName");
    

    您已经在td-tag 上设置了name 属性:

    <td name=prvlg>
    

    但只有表单元素被提交。

    编辑: 为了提交其他值,它们也应该在input
    您还想提交前两行:

    {out.print("<tr><td name=prvlg>"+rs1.getInt("FORM_ID")+"</td>"
           + "<td name=prvlg>"+rs1.getString("FORM_NAME")+"</td>");
    

    然后在表格单元格中添加隐藏输入:

    {out.print("<tr><td name=prvlg><input name='formId' type='hidden' value='" + rs1.getInt("FORM_ID") + "'></td>"
           + "<td name=prvlg><input name='formName' type='hidden' value='" + rs1.getString("FORM_NAME") + "'></td>");
    

    &lt;input type=hidden&gt; 在浏览器中不可见,但已提交。

    【讨论】:

    • 在此仅提交检查值。但是我怎么能提交未选中的复选框值呢?因为我需要将这些值传递给序列中的函数。
    • 没错。仅提交选中的复选框。为了处理未选中的值,您可以为每个复选框添加值为 true 或 false 的其他隐藏输入,并使用 javascript 将更改从复选框传输到隐藏输入。
    • 非常感谢。但是怎么做呢?由于字段是动态生成的,因此我无法为隐藏字段提供任何 ID。那么我该如何设置它们的值呢?
    • 你可以反过来。数据来自数据库,对吗?因此,您可以在提交表单后迭代所有列,通常用于生成 jsp 表。并在循环中检查提交了哪些复选框,其他所有复选框都是错误的。或类似的东西。
    • 有什么办法可以确定哪个复选框属于表格中选中的哪个单元格?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多