隐藏输入字段始终为空

时间:2014-06-29 18:32:52

标签: jsp servlets

我有一个DisplayMessage.jsp页面,用于打印存储在表格中的数据。我需要更新数量变量的值,并要求传递更改字段的值。 我试图通过使用隐藏的输入字段来实现这一点,但我总是在servlet中得到一个空值。 我的JSP页面是:

  <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
   <%@ page import="package1.GetConnection,java.sql.*" %>
   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"   "http://www.w3.org/TR/html4/loose.dtd">
   <html>
   <head>
   <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>Insert title here</title>
   <script type="text/javascript">
   function modify(button_id)
   {
  var text_val=document.getElementById('textbox'+button_id).value;
  var dep_id=document.getElementById("dep"+button_id).value;
  var machine_code=document.getElementById("machine"+button_id).value;
  var mtlcode=document.getElementById("material"+button_id).value;
  //alert("Text box value is "+text_val+"Department is "+dep_id+"Machine is"+machine_code+"Material code is "+mtlcode);
  //$.post("UpdateValue.jsp",{'button_id':button_id});

}



    </script>


    </head>

    <body>

     <form id="displayEstimate" name="displayEstimate" action="UpdateData" method="post" enctype="multipart/form-data">
     <input id="button_id" type="hidden" value="Hello"></input>
    <%
  GetConnection gc=new GetConnection();
  Connection con=gc.getConnection();
  PreparedStatement ps=con.prepareStatement("select   dep,machine_code,mtlcode,qty,value,to_char(date_of_entry,'DD-MON-YYYY') from tbl_material_estimation");
  ResultSet rs=ps.executeQuery();


  int i=0;
  out.print("<table style=\"border-style:solid\"; >");
  out.print("<tr><td width=\"50\">Modify</td><td width=\"50\">Delete</td><td width=\"70\">Department</td><td width=\"100\">Machine Code</td><td width=\"100\">Material Code</td><td width=\"50\">Quantity</td><td width=\"50\">Value</td><td width=\"100\">Date of Entry</td></tr>");
 while(rs.next())
    {
  out.print("<tr><td><button id=\""+i+"\" onclick=\"modify(id)\">Mod</button></td>");
  out.print("<td><button id=\""+i+"\">Del</button></td>");
  out.print("<td width=\"50\" align=\"center\">"+rs.getString(1)+"<input id=\"dep"+i+"\" type=\"hidden\" value=\""+rs.getString(1)+"\"></input></td>");
  out.print("<td width=\"70\" align=\"center\">"+rs.getString(2)+"<input id=\"machine"+i+"\" type=\"hidden\" value=\""+rs.getString(2)+"\"></input></td>");
  out.print("<td width=\"100\">"+rs.getString(3)+"<input id=\"material"+i+"\" type=\"hidden\" value=\""+rs.getString(3)+"\"></td>");
  out.print("<td width=\"50\" align=\"center\"><input type=\"text\" id=\"textbox"+i+"\" value=\""+rs.getString(4)+"\"><input type=\"hidden\" id=\"hidden"+i+"\" value=\""+rs.getString(4)+"\"></td>");
  out.print("<td width=\"50\" align=\"center\">"+rs.getString(5)+"</td>");
  out.print("<td width=\"100\">"+rs.getString(6)+"</td></tr>");
i++;
  }

  out.print("</table>");
   %>
  </form>
 </div>

 </body>
 </html>

我的Servlet代码如下:

  import java.io.IOException;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;

  public class UpdateData extends HttpServlet {


   public UpdateData() {
     super();
    // TODO Auto-generated constructor stub
    }

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    String id=request.getParameter("button_id");
    System.out.println("Id is "+id);


}

}

我尝试打印一个简单的隐藏字段的值,但这也给了我null。有人可以指出出了什么问题吗?

1 个答案:

答案 0 :(得分:1)

如果发布了大量代码,很难确定问题,但是在你的情况下问题是

<input id="button_id"

你应该使用

<input id="button_id" name="button_id"

提交表单时,

HTML表单会占用name字段而不是id