多个依赖下拉jsp

时间:2012-02-29 14:37:39

标签: javascript jsp

我想创建多个下拉列表,在数据库中获取它们的值并相互依赖...

我在网上搜索,我能够得到一个示例代码,它适用于我,但它只有2个下拉菜单...我希望学习如何创建3个或更多下拉菜单或复制下拉..

这是代码

selDept.jsp

<%@page import="java.sql.*"%>
 <html>
      <head>
      <script language="javascript" type="text/javascript">
      var xmlHttp
      var xmlHttp
      function showEmp(str){
      if (typeof XMLHttpRequest != "undefined"){
      xmlHttp= new XMLHttpRequest();
      }
      else if (window.ActiveXObject){
      xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
      }
      if (xmlHttp==null){
      alert("Browser does not support XMLHTTP Request")
      return;
      }
      var url="selEmp.jsp";
      url +="?img_hub=" +str;
      xmlHttp.onreadystatechange = stateChange;
      xmlHttp.open("GET", url, true);
      xmlHttp.send(null);


      }

      function stateChange(){
      if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
      document.getElementById("country").innerHTML=xmlHttp.responseText
      }
      }


      </script>
      </head>
      <body>
      <select name='dept' onchange="showEmp(this.value)">
       <option value="none">Select</option>
    <%
 Class.forName("org.postgresql.Driver").newInstance();
 Connection con = DriverManager.getConnection("jdbc:postgresql://10.106.153.85:5432/EDPportal","postgres","qqQQ11!!");
 Statement stmt = con.createStatement();
 ResultSet rs = stmt.executeQuery("Select distinct img_hub from ifs_db");
 while(rs.next()){
     %>
      <option value="<%=rs.getString("img_hub")%>"><%=rs.getString("img_hub")%></option>
      <%
 }
     %>
      </select>
      <br>
      <div id='country' style="background-color:#FFA500";>
      <select name='country' >
      <option value='-1'></option>
      </select>
      </div>

      </body>
      </html>

selEmp.jsp

    <%@page import="java.sql.*"%>
<%
String hub =request.getParameter("img_hub");
 String buffer="<select name='country' ><option value='-1'>Select</option>";
 try{
 Class.forName("org.postgresql.Driver").newInstance();
 Connection con = DriverManager.getConnection("jdbc:postgresql://10.106.153.85:5432/EDPportal","postgres","qqQQ11!!");
 Statement stmt = con.createStatement();
 ResultSet rs = stmt.executeQuery("Select distinct country from ifs_db where img_hub ='"+hub+"' ");
   while(rs.next()){
   buffer=buffer+"<option value='"+rs.getString(1)+"'>"+rs.getString("country")+"</option>";
   }
 buffer=buffer+"</select>";
 response.getWriter().println(buffer);
 }
 catch(Exception e){
     System.out.println(e);
 }
 %>

0 个答案:

没有答案