利用js數組生成省市二級聯動

  1. <script>
  2. var temp;
  3. function test(){
  4. //初始化一級聯動
  5. //***************自定義******************//
  6. temp = [
  7. ['湖北','武漢','黃石','孝感'],
  8. ['北京','海淀','朝陽']
  9. ];
  10. //*************從後臺取*******************//
  11. temp = new Array(count); 
  12.  for(var a=0;a<count;a++){
  13.  temp[a] = new Array(); 
  14.      }
  15.  <%
  16.  String[][] ta =  new dao().getAllValue();
  17.  for(int b =0;b<ta.length;b++){
  18.  for(int c =0;c<ta[b].length;c++){
  19.  %>
  20.  temp[<%=b%>][<%=c%>] = '<%=ta[b][c]%>'
  21.  <%
  22.  }
  23.  }
  24.  %>
  25. //******************************************
  26. for (var i = 0; i < temp.length; i++){
  27.  var newElem = document.createElement("option");
  28.  newElem.text=temp[i][0];
  29.  newElem.value=temp[i][0];
  30.  document.getElementById("ctrlList").options.add(newElem);
  31. }
  32. choose(0);
  33. }
  34. //選擇後聯動
  35. function choose(num){
  36.  deleteRecord();
  37.  for (var j = 1; j < temp[num].length; j++){
  38.  var newElem = document.createElement("option");
  39.  newElem.text=temp[num][j];
  40.  newElem.value=temp[num][j];
  41.  document.getElementById("cityList").options.add(newElem);
  42. }
  43. }
  44. //刪除上節點
  45. function deleteRecord(){
  46.  var obj = document.getElementById("cityList");
  47.  var count = obj.length;
  48.  for(var k=(count-1);k>=0;k--){
  49.  obj.options.remove(k); 
  50.  }
  51.  }
  52. </script>
  53. </head>
  54. <body οnlοad="test()">
  55.   <select size="1" name="ctrlList" οnchange="choose(this.selectedIndex)">
  56.   </select>
  57.   <select size="1" name="cityList">
  58.   </select>
  59. </body>
  60. //==============================後臺讀取數據庫組織數組==================================//
  61. package tt;
  62. import java.sql.Connection;
  63. import java.sql.ResultSet;
  64. import java.sql.SQLException;
  65. import java.sql.Statement;
  66. import java.util.ArrayList;
  67. import java.util.Iterator;
  68. import java.util.List;
  69. public class dao {
  70.  private static Connection con;
  71.  private static Statement sta;
  72.  private static ResultSet rs;
  73.  private static String[][] temp;
  74.  public int getProvince() throws SQLException{
  75.   List list = new ArrayList();
  76.   con = context.getCon();
  77.   sta  = con.createStatement();
  78.   rs = sta.executeQuery("select province from zip group by province");  
  79.   while(rs.next()){
  80.    list.add(rs.getString(1));
  81.   }
  82.   closeData();
  83.   temp = new String[list.size()][];
  84.   for(int i=0;i<list.size();i++){
  85.    temp[i] = new String[]{(String)list.get(i)};
  86.   }
  87.   return temp.length;  
  88.  }
  89.  public String[][] getAllValue() throws SQLException{  
  90.   int count = getProvince();
  91.   for(int i=0;i<count;i++){
  92.    List list = new ArrayList();
  93.    con = context.getCon();
  94.    sta  = con.createStatement();
  95.    rs = sta.executeQuery("select city from zip where province='"+temp[i][0]+"'");
  96.    while(rs.next()){    
  97.     list.add(rs.getString(1));
  98.    }
  99.    String[] a = new String[list.size()+1];
  100.    for(int j=1;j<=list.size();j++){
  101.     a[0] = temp[i][0];
  102.     a[j] = (String)list.get(j-1);
  103.    }
  104.    temp[i] = a;
  105.   }
  106.   closeData();
  107.   return temp;    
  108.  }
  109.  public static void closeData() throws SQLException{
  110.   rs.close();
  111.   sta.close();
  112.   con.close();
  113.  }
  114.  public static void main(String[] args) throws SQLException {
  115.   String[][] te = new dao().getAllValue();
  116.   for(int i=0;i<te.length;i++){
  117.    
  118.    for(int j=0;j<te[i].length;j++){
  119.     System.out.print(te[i][j]);
  120.    }
  121.    System.out.println("/n");
  122.   }
  123.   
  124.   
  125.   
  126.  }
  127. }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章