Board logo

标题: jsp+javascript打造级连菜单 [打印本页]

作者: admin    时间: 2007-12-5 17:43     标题: jsp+javascript打造级连菜单

<%@ page import="java.util.date,yava.fileapp.*,java.sql.*;"
: B$ A5 L& ^) ]" G/ R         c pageencoding="gb2312"
: m% L. N. `: ]- s%>
( I- s  Z. b2 j$ {9 Y<style>' ?- f" G( L4 P7 ?/ H
.f9{ font-size:9pt; }
! v/ V2 _( d% o.bgc{ background-color:#aecaf9; color: #0033ff }  }5 q! T# J* h0 ~7 a/ w
.buttons{font-family:arial; font-size:13px; font-weight:bold; background-color:#6796e4; color:white; border-top: solid 2px #aacafb;
8 j% C$ }2 d! X; {; t* D  border-bottom: solid 1px #4e7dc1;
+ c# G; B& X4 A4 c3 M7 A7 D  border-left: solid 1px #aecaf9;
  }) W0 F- ]5 o5 z9 {& j  border-right: solid 1px #5679bd;" o. @0 m1 @* }; K$ b6 |! u3 i3 \
  padding:1px;, k/ w" r; `1 F- N3 O6 X; o  S6 e
  margin:0px;}2 v7 ?5 T# s+ z; _4 }
</style>  r& W) [' y  E
<script language="javascript">8 a, j2 N) x2 E8 d4 [3 V
<!--
7 ?% T2 o# h* s0 ]) R' B/ ^' ifunction rv()3 f7 q! X( k- _+ {5 L! q
{
  u$ |4 f. `5 u8 @  var val="";. n& G% Y) X0 o5 b; x; V5 G
  for(i=0;i<combo_box.list2.length;i++){  P8 C9 G' R, \0 U
   val+=","+combo_box.list2.value;; Q( m/ J  i4 f1 t
  }2 Q/ T& o7 [# D! P, N% n% H
  if(val.charat(0)==","){6 f. }. f$ R0 u& e, ^  [
   val=val.substr(1,val.length);
1 E' _6 w% Y& r" }5 H6 \  }! N! |( R4 G+ T* O
  opener.form1.frecname.value=val;
- N7 ~! Q% F6 g) I8 P  self.close();
3 ]$ w9 h$ |. H$ H}- t2 d/ N. J7 c
//-->9 p. I! V0 K: s: X5 z& S
</script>7 b7 T* a0 s1 }. V7 z" P  b" w
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
3 Z" z2 D/ L0 C$ X; w2 e5 q<jsp:usebean id="user" scope="session" class="yava.fileapp.userbean" />
( T. z% Y) x$ ^) ]% j<%3 {' k# u) u; M# f$ c- W
  cdatasource ds=new cdatasource();  //数据联结bean实例
8 }& [, n2 y' U4 a  java.sql.connection conn=ds.getconnection();
, D* X) |0 |- F$ @8 j  java.sql.statement stmt=null;9 M: `& o4 N3 @6 T8 P
  java.sql.resultset rs=null;% a' b$ r$ J( j: q8 C& H) c$ v
  cdatacheck dc=new cdatacheck();
+ r% Z: S% c3 ?* D%>) }$ S' M0 D0 \4 E1 h
<%7 L/ N# t% N' k% [: p1 R
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";3 \4 l+ U7 z) \
stmt=conn.createstatement();
% E$ T. v6 d9 b) z2 i' R6 Krs=stmt.executequery(sqlu);, W% n0 V1 o3 n: f. A3 i
%>0 j4 J1 y3 z( }9 E( Z* Y8 j
<script language='javascript'>
1 }9 b8 v$ B; O  ]! m) N$ S( l" {arr = new array();
& t6 \1 X; `. k( \3 y& \- w<%  int temp=0;
# v0 H3 f0 S8 t) h' cwhile(rs.next())( r* S3 Y! i% q* A
{0 l6 \" M, _! U# a/ V: y$ K& [
%>
& \% R6 U3 l0 _( l/ o% t. g6 jarr[<%=temp%>]=new array("<%=rs.getstring("fname")%>","<%=rs.getstring("fdept")%>");
6 |- m1 O: n5 Y3 E) J2 l) m8 j<%
6 ?" ~( L; A0 L1 ftemp = temp + 1;
. L+ \* T  r6 N/ ~}0 r4 y& k  }! t5 h- Z9 w
%>
; E3 Y- B% C  K* Y$ A/ N2 t. Ftemp=<%=temp%>;
, W6 c" c/ Y- Qfunction changelocation(id){
/ g  J1 W( `" u! e) rdocument.combo_box.city.length=0; //初始化第2级菜单的长度,下标从0开始
7 b4 f( m2 O1 ?$ h  \' `var i = 0;
& F' n/ C* }# J' f; V8 E1 Vdocument.combo_box.city.options[0]=new option('-------',''); ; [9 Z' j" H7 B0 [7 t/ L
for(i=0;i<temp;i++){ $ u# v% t6 W6 k2 `5 o5 D
if(arr[1]==id){//如果相等,证明在第2级里面有输入第1级组织的子集,arr[总数目][部门]
% ]% j3 g; U" idocument.combo_box.city.options[document.combo_box.city.length] = new option(arr[0], arr[0]);
# y7 M4 ?  S  H& z  m4 K/ S# Q}
9 u& Y" c9 M2 Q. \+ M}
$ r5 O3 Q, N2 a, c}
, i, l) D. y: Z$ T4 T</script>4 {: b/ f  Z0 [, K1 I% ]" [1 Q

* C* h9 z- P" \/ m<form name="combo_box">
+ U5 o5 [0 V# C7 e<table border="0" cellspacing="0" cellpadding="0" height="210" width="59">
" _% G4 n% y6 I. @! `( ]  <tr height="24">5 Y# r. v# y# R4 s. \* L% s/ f* _
    <td bgcolor="#336699" class="buttons" align=center><font color=#ffffff>部门选择</font></td>% M- t' }, \+ \$ B! X
    <td bgcolor="#336699" class="buttons" align=center><font color=#ffffff>人名选择</font></td>
6 d) Y: {1 H4 z+ `& `, ~" N    <td bgcolor="#336699" class="buttons" align=center nowrap><font color=#ffffff>添加/移除</font></td>
& D0 N# X5 {2 }' L: h    <td bgcolor="#336699" class="buttons" align=center><font color=#ffffff>最终人</font></td>, b) q- k3 j2 h5 K2 R
  </tr>7 l4 B0 G0 i) g9 [, w  t, W
  <tr> - O, I) T' {# T
    <td>% k5 _9 \. n/ Y% k$ h- O
  <select multiple id=prov name="prov"    style="width:150;height:200" class="bgc">
/ x) t3 t1 V" r/ ^3 d  <option value="0">请选择部门 ---></option>, ?1 W; w# @" O5 F8 W1 C' c
<%* |% m  v0 Y/ e' z/ T# g
    string sqld="select * from tdept";
0 v  D" ~4 A- o( l* ]- U' |" xstmt=conn.createstatement();# q, e& d+ n& Z7 d2 ~
rs=stmt.executequery(sqld);8 k1 W, t& n( e" }: H5 D# A
while(rs.next())
/ ?1 u: I$ V- B6 ?) F- `{. J9 U; j# ^/ F9 {6 F
%>
  l1 r+ W% j7 I! C% ~! f5 w5 `  D  <option name="<%=rs.getstring("fno")%>"><%=rs.getstring("fname")%></option>
& ?$ t3 ~! a  R<%; |0 F  O5 |# C; y/ U
}
2 ~% e* H/ h$ G( W$ s3 o%>
' M8 F, H2 D% Y) v5 m; Z  </select>
7 D" t; f1 [$ K& O  ]/ X</td>" S7 K( Y1 F$ e( f! e- o1 G
    <td>
0 i# l" o0 N$ m! t7 J  <select multiple id=city style="width:150;height:200" class="bgc">
. P0 K' T  ]6 G4 ]+ D  </select>$ w" w; {4 @7 Z! y
</td>
2 K' u& E+ `% A' R: H' o" N    <td nowrap align="center" class="bgc">
, X$ b2 P1 g( w( E  <input type="button"  value="<<" class="buttons">
% e4 D( n! B) ^  <input type="button"  value=">>" class="buttons">1 A! o6 d4 c% \6 t
</td>- L/ q! _, ^$ P! v# b. ^* h4 x7 f% J# v
    <td>
% U6 K2 p! B/ O' i  <select multiple size="10" name="list2" style="width:150;height:200" class="bgc">
, G% K1 P  G3 N' S5 g2 p  </select>
9 f% b1 `  Z4 a) e: Y</td>+ _8 B# H* A4 A% k
  </tr>
& z* v4 u- P9 @    <tr class="bgc"> 5 `. G# f0 t0 e# E  c
    <td colspan="4" align="center"><input type="button" name="button1" class="buttons" value="选好了!" ></td>8 _$ `5 P$ f- f0 m) O8 B( @
  </tr>
  a; {5 M5 R: U/ W+ f</table>
, w0 o" @2 ~6 k/ h+ _3 s; w4 q</form>
$ X2 T) J* X! ^/ c<script language="javascript">
. B/ V2 W% c; N- H# _# n6 h! b# Y//人名移动; m' Y/ G$ I; o: |4 l
function move(fbox, tbox) {
" m: d: o# c1 ~' n: |1 nvar arrfbox = new array();) k5 Q4 G( ]5 t1 D
var arrtbox = new array();
$ t. |2 G; |% ~; q- _6 _& Z$ `var arrlookup = new array();: u8 y3 S# O* `8 C' d) N/ L
var i;
7 ]8 Y# `* q$ m& s$ Z+ J% qfor (i = 0; i < tbox.options.length; i++) {" t2 D, u- U& z) A, i
arrlookup[tbox.options.text] = tbox.options.value;
9 c  U# n5 }. l: R7 N, Barrtbox = tbox.options.text;) R, ?7 l) g2 F9 j( ^. a. S$ `: E
}
* J7 h; _5 g! n6 a3 dvar flength = 0;
) }* E7 z3 o; N; |var tlength = arrtbox.length;, u+ T) U% N7 j7 Z6 {4 a
for(i = 0; i < fbox.options.length; i++) {. ]) c- \! s$ H* l$ ^
arrlookup[fbox.options.text] = fbox.options.value;
4 m5 N7 j6 Y' K5 C9 qif (fbox.options.selected && fbox.options.value != "") {
# [$ ?' r4 c( rarrtbox[tlength] = fbox.options.text;
4 Z( \8 T8 t6 ptlength++;) |1 a8 g. ?9 L# M5 w9 E. p
}
7 A5 c' p+ W3 |! B9 N2 c( w" Helse {2 _. p/ p* y0 R8 r* ?
arrfbox[flength] = fbox.options.text;2 t% B) ^+ K9 I# D) A
flength++;: ]! m& d+ k6 p7 w. F
   }9 c2 N  E* `* d6 c7 k
}! r2 ]( o9 H" G; w# [  X; `- l! H- l
arrfbox.sort();
: u: x0 a4 |& W0 d" garrtbox.sort();
) s# t' ]# F# H8 [7 R
0 {  r' P. x1 r3 L7 c3 }. U6 x5 Gfbox.length = 0;( b; `! a6 ^) d) x% }
tbox.length = 0;
/ q$ i( _2 b7 s3 u/ Zvar c;
( r# h% k. J' N! T6 nfor(c = 0; c < arrfbox.length; c++) {- d0 ^0 n( a6 @/ G& _6 `
var no = new option();
& ~7 A1 c4 Y3 a6 q+ C) nno.value = arrlookup[arrfbox[c]];. x+ p5 V/ D& h2 e) m
no.text = arrfbox[c];
. p* b2 C, M# }& C8 Lfbox[c] = no;  F0 T5 _7 E' L9 G
}
, i2 M8 |* F7 `" j" gfor(c = 0; c < arrtbox.length; c++) {
. f" t7 p9 C' G4 X9 g4 ]var no = new option();
" y+ I5 D. w- c) t1 dno.value = arrlookup[arrtbox[c]];
& D5 P* u- Y% F3 A, N( Cno.text = arrtbox[c];1 t" b4 R/ U' F0 b  n4 }
tbox[c] = no;9 m6 V, r" H: x
   }3 \' f6 I( R/ X( `2 f
}
5 N1 p, F! x1 o& V2 |! n& j# r</script>
6 z4 ]/ L2 _- j</body>
% j+ g* ^- H4 [, a





欢迎光临 捌玖网络工作室 (http://89w.org/) Powered by Discuz! 7.2