提交后如何在jsp中保留下拉列表的值

时间:2015-05-04 04:33:23

标签: html jsp

我想在第一个下拉列表中保留值。我应该用什么来保留我从下拉列表中提交的值?我希望选择批处理代码参数值。

这是我迄今为止尝试过的代码。

<%@page import="com.database.DatabaseConnection"%>
<%@page import="com.model.StudentRegOperation"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.util.*" %>    
<%@ page import="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>
</head>
<body>
<form  method="post" action="">
<% 
                DatabaseConnection db (DatabaseConnection)getServletContext().getAttribute("connection");
                Connection con=db.getConnection();
                StudentRegOperation so=new StudentRegOperation();
                so.setConnection(db);
                Vector<String> data=so.showBatches();
                Iterator<String> itr=data.iterator();
                 %>
<select name="batchCode" id="batchCode" onchange="this.form.submit()" >
   <option>Batches</option>
            <%
                 while(itr.hasNext())
                {
                %>
                    <option <%if(request.getParameter("batchCode")!=null){%>  selected="selected" <%} %> > <%=itr.next() %></option>                   
               <%
                }
               %>
        </select>  
<%
  String batchCode=request.getParameter("batchCode");
  if(batchCode!=null)
  {
  Vector<String> batches=so.showRegId(batchCode);
  Iterator<String> itr2=batches.iterator();
%>
<br> <select name="regId" id="regId" onchange="this.form.submit()" >
  <%
  while(itr2.hasNext())
  {%>
     <option><%=itr2.next() %></option>
 <%  
  }%>
  </select>
 <%
 }
else
{
   %>
   <br><select ><option>Roll No</option></select>
 <%
}
 %>


</form>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

您可以这样写:

<%
  while(itr2.hasNext())
  {
    String elem = itr2.next().toString(); 
    if(request.getParameter("regId")!=null && request.getParameter("regId").equals(elem)) {
%>
     <option selected><%=elem %></option>
 <%  
    } else {
 %>
       <option><%=elem %></option>   
 <%    }
}%>