服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > J2EE > Servlet/Jsp > 查看文档

实例演示:jsp+javascript打造级连菜单

  <%@ page import="java.util.date,yava.fileapp.*,java.sql.*;"
         contenttype="text/html;charset=gb2312" pageencoding="gb2312"
  %>
  <style>
  .f9{ font-size:9pt; }
  .bgc{ background-color:#aecaf9; color: #0033ff }
  .buttons{font-family:arial; font-size:13px; font-weight:bold; background-color:#6796e4; color:white; border-top: solid 2px #aacafb;
    border-bottom: solid 1px #4e7dc1;
    border-left: solid 1px #aecaf9;
    border-right: solid 1px #5679bd;
    padding:1px;
    margin:0px;}
  </style>
  <script language="javascript">
  <!--
  function rv()
  {
    var val="";
    for(i=0;i<combo_box.list2.length;i++){
     val+=","+combo_box.list2[i].value;
    }
    if(val.charat(0)==","){
     val=val.substr(1,val.length);
    }
   opener.form1.frecname.value=val;
    self.close();
  }

  //-->
  </script>
  <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
  <jsp:usebean id="user" scope="session" class="yava.fileapp.userbean" />
  <%
    cdatasource ds=new cdatasource();  //数据联结bean实例
    java.sql.connection conn=ds.getconnection();
    java.sql.statement stmt=null;
    java.sql.resultset rs=null;
    cdatacheck dc=new cdatacheck();
  %>

  <%
   string sqlu="select t1.fno, t1.fname, t2.fname as fdept from tuser t1 left outer join tdept t2 on t1.fdept = t2.fno order by t2.fname";
   stmt=conn.createstatement();
   rs=stmt.executequery(sqlu);
  %>
   <script language='javascript'>
   arr = new array();
  <%  int temp=0;
   while(rs.next())
   {
  %>
   arr[<%=temp%>]=new array("<%=rs.getstring("fname")%>","<%=rs.getstring("fdept")%>");
  <%
   temp = temp + 1;
   }
  %>
  temp=<%=temp%>;
  function changelocation(id){
  document.combo_box.city.length=0; //初始化第2级菜单的长度,下标从0开始
  var i = 0;
  document.combo_box.city.options[0]=new option('-------','');
  for(i=0;i<temp;i++){
  if(arr[i][1]==id){//如果相等,证明在第2级里面有输入第1级组织的子集,arr[总数目][部门]
  document.combo_box.city.options[document.combo_box.city.length] = new option(arr[i][0], arr[i][0]);
  }
  }
  }
  </script>

  <form name="combo_box">
  <table border="0" cellspacing="0" cellpadding="0" height="210" width="59">
  <tr height="24">
    <td bgcolor="#336699" class="buttons" align=center><font color=#ffffff>部门选择</font></td>
    <td bgcolor="#336699" class="buttons" align=center><font color=#ffffff>人名选择</font></td>
    <td bgcolor="#336699" class="buttons" align=center nowrap><font color=#ffffff>添加/移除</font></td>
    <td bgcolor="#336699" class="buttons" align=center><font color=#ffffff>最终人</font></td>
  </tr>
  <tr>
    <td>
    <select multiple id=prov name="prov"  onchange="changelocation(combo_box.prov.options[combo_box.prov.selectedindex].text);"  style="width:150;height:200" class="bgc">
  <option value="0">请选择部门 ---></option>
  <%
    string sqld="select * from tdept";
   stmt=conn.createstatement();
   rs=stmt.executequery(sqld);
   while(rs.next())
   {
  %>
  <option name="<%=rs.getstring("fno")%>"><%=rs.getstring("fname")%></option>
  <%
   }
  %> 
    </select>
   </td>
      <td>
    <select multiple id=city style="width:150;height:200" class="bgc">
    </select>
   </td>
      <td nowrap align="center" class="bgc">
  <input type="button" onclick="move(this.form.list2,this.form.city)" value="<<" class="buttons">
  <input type="button" onclick="move(this.form.city,this.form.list2)" value=">>" class="buttons">
 </td>
    <td>
  <select multiple size="10" name="list2" style="width:150;height:200" class="bgc">
  </select>
 </td>
  </tr>
    <tr class="bgc">
    <td colspan="4" align="center"><input type="button" name="button1" class="buttons" value="选好了!" onclick="rv();"></td>
    </tr>
  </table>
  </form>

  <script language="javascript">   //人名移动
  function move(fbox, tbox) {
  var arrfbox = new array();
  var arrtbox = new array();
  var arrlookup = new array();
  var i;
  for (i = 0; i < tbox.options.length; i++) {
  arrlookup[tbox.options[i].text] = tbox.options[i].value;
  arrtbox[i] = tbox.options[i].text;
  }
  var flength = 0;
  var tlength = arrtbox.length;
  for(i = 0; i < fbox.options.length; i++) {
  arrlookup[fbox.options[i].text] = fbox.options[i].value;
  if (fbox.options[i].selected && fbox.options[i].value != "") {
  arrtbox[tlength] = fbox.options[i].text;
  tlength++;
  }
  else {
  arrfbox[flength] = fbox.options[i].text;
  flength++;
     }
  }
  arrfbox.sort();
  arrtbox.sort();
  fbox.length = 0;
  tbox.length = 0;
  var c;
  for(c = 0; c < arrfbox.length; c++) {
  var no = new option();
  no.value = arrlookup[arrfbox[c]];
  no.text = arrfbox[c];
  fbox[c] = no;
  }
  for(c = 0; c < arrtbox.length; c++) {
  var no = new option();
  no.value = arrlookup[arrtbox[c]];
  no.text = arrtbox[c];
  tbox[c] = no;
     }
  }

  </script>
  </body>

扫描关注微信公众号