联动select选项的选定

 联动select的选定

<%@ page language="java" contentType="text/html; charset=gbk"
    pageEncoding="gbk"%>
<%@ page import="java.sql.*"%>
<%@ page import="my.bbs.connections.DBConn"%>
<%@ page import="my.bbs.com.ConvertString" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>添加汽车的品牌种类(//联动select的选定)</title>
<script language="javascript">
<!--
       //先从数据库中取出数据,再构造这个联动的select菜单
 var onecount;
 onecount=0;
 //var j;
  
 subcat = new Array();
 <%
 ConvertString convert = new ConvertString();
 String sql="select ct_id,ct_pid,ct_name from car_type where ct_pid<>0";
 DBConn dbConn = new DBConn();
 Connection conn1 = dbConn.getConn();
 Statement stmt1 = conn1.createStatement();
 ResultSet rs = stmt1.executeQuery(sql);
 int j=0;
 while(rs.next()) {
 %>
 subcat[<%=j%>] = new Array("<%=convert.convertString(rs.getString("ct_name"))%>","<%=rs.getInt("ct_pid")%>","<%=rs.getInt("ct_id")%>");
 <%
 j=j+1;
 }
 if(rs!=null) {
  rs.close();
 }
 if(stmt1!=null) {
  stmt1.close();
 }
 if(conn1!=null) {
  conn1.close();
 }
 %>
 
 onecount=<%=j%>;
 
 function changelocation(locationid)
     {
     document.carTypeForm.ct_id.length = 0;
 
     var locationid=locationid;
     var i;
     document.carTypeForm.ct_id.options[0] = new Option('==分种类==','');
     for (i=0;i < onecount; i++)
         {
             if (subcat[i][1] == locationid)
             {
             document.carTypeForm.ct_id.options[document.carTypeForm.ct_id.length] = new Option(subcat[i][0], subcat[i][2]);
             }       
         }
        
     }   
 
 //-->
</script>
</head>
<body>
<%!
//显示所有的汽车种类
 String str2="";
ConvertString convert = new ConvertString();
 private void showOptions2(Connection conn) {

 Statement stmt = null;
 ResultSet rs = null;
 try {
  stmt = conn.createStatement();
  String sql = "select * from car_type where ct_isparent=1 and ct_pid=0";
  rs = stmt.executeQuery(sql);

  while (rs.next()) {
   str2 += "<option value='" + rs.getInt("ct_id") + "'>"
     + convert.convertString(rs.getString("ct_name")) + "</option>";
  }
 } catch (SQLException e) {
  e.printStackTrace();
 } finally {
  try {
   if (rs != null) {
    rs.close();
    rs = null;
   }
   if (stmt != null) {
    stmt.close();
    stmt = null;
   }
   if(conn!=null) {
    conn.close();
    conn = null;
   }
  } catch (SQLException e) {
   e.printStackTrace();
  }
 }
}

  %>
<%
  DBConn dbConn2 = new DBConn();
  Connection conn = dbConn2.getConn();
  str2="";   //显示种类
  showOptions2(conn);
 

%> 
<form atcion="" method="post" name="carTypeForm">
<table>
 <tr>
    <td>所属种类</td>
    <td><select name="ct_pid" onchange="changelocation(document.carTypeForm.ct_pid.options[document.carTypeForm.ct_pid.selectedIndex].value)">
         <option value="">==主种类==</option>
         <%=str2%></select></td>
  </tr>
    <tr>
    <td>所属种类系列</td>
    <td><select name="ct_id" ></select></td>
  </tr>
</table>
</form>
</body>
</html>

你可能感兴趣的:(联动select选项的选定)