2015-05-04 124 views
2

我想在第一个下拉列表中保留值。我应该如何保留从下拉列表中提交的值?我希望批量代码参数值应该被选中。如何在提交后保留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> 

回答

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> 
<% } 
}%> 
+0

我想第一选择标记的那个值当我选择任何选项 –

+0

thankuu提交应保留,。我知道了 –

+0

@MohitJindal wc ..请接受答案,如果它帮助.. –

相关问题