Board logo

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

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

<%@ page import="java.util.date,yava.fileapp.*,java.sql.*;"
( T7 ?- w2 M* ^" o/ b: b( H         c pageencoding="gb2312"
$ F* I/ f7 G* i, w%>
( _: Q' F3 i" v- g4 n3 G. d<style>
0 n$ s: j! H4 n9 D.f9{ font-size:9pt; }' C$ _! I: q6 z) m$ V& Y$ `
.bgc{ background-color:#aecaf9; color: #0033ff }, b/ G6 ?( J4 v  _& f! p
.buttons{font-family:arial; font-size:13px; font-weight:bold; background-color:#6796e4; color:white; border-top: solid 2px #aacafb;
  l7 z4 d3 A9 e; P7 u  border-bottom: solid 1px #4e7dc1;+ i; _( V; t& d: R7 `2 \4 X; {
  border-left: solid 1px #aecaf9;
8 I6 o: e" e1 b- ^5 m, X  border-right: solid 1px #5679bd;- B8 u$ V/ R8 e8 Z  L- g
  padding:1px;
* B8 D/ N) U  h6 q5 |  margin:0px;}
* M- c6 K" a2 p$ p</style>
: B7 p  O$ x, j  G) l- c<script language="javascript">
4 _/ R2 h/ _% d& Z3 h<!--# Q' ?$ u- s( g
function rv()% m# o+ G0 B: j! q
{
8 \7 p# a% |" J: ^8 y3 C  var val="";
5 p) Q9 u4 k0 |  c; B# U  for(i=0;i<combo_box.list2.length;i++){
4 x3 f7 B9 E1 t) t0 }   val+=","+combo_box.list2.value;1 H+ P9 j% j1 T9 \6 y$ x, w: G# _
  }( o7 X" @" J9 {" J
  if(val.charat(0)==","){! M2 [( Z5 a, V8 I/ G3 F, G
   val=val.substr(1,val.length);! a3 Y! o4 X9 f- c, |
  }
+ \8 r! b$ ~; M; l) M5 P  opener.form1.frecname.value=val;- w9 i9 z4 J: l. ^2 f) n! {' ?9 i
  self.close();+ A4 E  k' I1 s2 q
}
, G8 U* d# M; u5 N3 F# N! u' r//-->& L4 L  H3 {$ J" b  g8 e
</script>
- J, a; C. {4 f$ R<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
3 B! k& n6 H+ ?9 r3 T<jsp:usebean id="user" scope="session" class="yava.fileapp.userbean" />
* E1 ~& z* r  G<%
- [. W7 |& z, o! x0 Y0 U% X3 s# p  cdatasource ds=new cdatasource();  //数据联结bean实例' r  z  ~7 L) _0 V0 C2 o
  java.sql.connection conn=ds.getconnection();
- q! j. Y+ V; M4 H, V8 }2 X' P  java.sql.statement stmt=null;8 A- L( B. |* p, O* W/ O, W
  java.sql.resultset rs=null;4 {8 N5 ^8 V0 J& }6 j* f4 I" j. n
  cdatacheck dc=new cdatacheck();- _& b; S% k- P
%>( w# [/ y6 K- T
<%
9 D; z2 r# z$ t; cstring 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";8 g4 Q" i  a' |# k
stmt=conn.createstatement();' O1 K2 y+ e" V1 u, N7 l* ~9 U1 ^0 k
rs=stmt.executequery(sqlu);
+ R; O6 q! e' ~4 W%>
% p+ t8 S0 h5 _- m( I" {<script language='javascript'>, {* M4 B& A7 h$ b! n$ L  s
arr = new array();0 E' _* A1 ?8 O& a0 E( P
<%  int temp=0;" l! v, |( o$ l1 N) v: P! E% Z+ f
while(rs.next())
& d6 c7 }( S! N* L) s{0 H; a# d: I) I' ~5 ?
%>" @) |* Y+ c) q
arr[<%=temp%>]=new array("<%=rs.getstring("fname")%>","<%=rs.getstring("fdept")%>");+ g" O" z, n7 _( a, p8 {
<%
+ `& {" f7 r! {temp = temp + 1;3 o7 ?# t( n5 b# b& [
}' G3 O' u1 R2 }3 m! b$ ~) x0 @
%>
! b5 n' H: o  w5 q1 e$ @temp=<%=temp%>;& I' r/ t5 _- H1 x0 ^5 ?
function changelocation(id){
+ ]4 K8 x% L- J  kdocument.combo_box.city.length=0; //初始化第2级菜单的长度,下标从0开始 & M% m: f* q, ~7 t) ]# j
var i = 0;   f( i( S' g. r* M) `) _2 ^# h
document.combo_box.city.options[0]=new option('-------',''); - H0 O+ `6 _$ t) i- m& Y9 H
for(i=0;i<temp;i++){ 2 c5 W# E! @  x; q" S
if(arr[1]==id){//如果相等,证明在第2级里面有输入第1级组织的子集,arr[总数目][部门]
' J4 ]; A) I8 g: O% o  b& _; J% K9 Tdocument.combo_box.city.options[document.combo_box.city.length] = new option(arr[0], arr[0]); 0 @8 K5 a$ n3 _% M( m, q
} 8 ^0 c/ [/ G; c2 e2 z! E
} ! |0 D  E6 f- _* L# s' L% ]9 J
} ! j9 r7 n) U2 t) N& ~
</script>
. K; q: H6 Z- C% G8 p  @) C: y" ~: a1 ]0 D$ ?% v. j
<form name="combo_box">8 P; k! G/ B& e
<table border="0" cellspacing="0" cellpadding="0" height="210" width="59">' X- ]7 ^, N! W% P+ O7 S. |; `2 ^
  <tr height="24">) g) n9 F7 E0 |
    <td bgcolor="#336699" class="buttons" align=center><font color=#ffffff>部门选择</font></td>
" ^8 V$ }  k) c: k. ?    <td bgcolor="#336699" class="buttons" align=center><font color=#ffffff>人名选择</font></td>
- ~# }% b% v+ S# {    <td bgcolor="#336699" class="buttons" align=center nowrap><font color=#ffffff>添加/移除</font></td>
& i5 S8 W. r7 C5 L7 i    <td bgcolor="#336699" class="buttons" align=center><font color=#ffffff>最终人</font></td>
7 W  p, P7 X' w# l+ Y  </tr>- n- O& e! h* {* e0 X! H! a# X7 i
  <tr> $ s4 i# n2 y1 T
    <td>
) u2 d0 n! n1 I! E# B7 s2 a  <select multiple id=prov name="prov"    style="width:150;height:200" class="bgc">1 j( M& w$ L+ N% j6 ~, e
  <option value="0">请选择部门 ---></option>
4 A% ^; {! Y& K, i. B# d+ U<%
, s- X5 E8 y! z0 G    string sqld="select * from tdept";
2 n, ^# e2 a& C% h, i% l/ N2 G) fstmt=conn.createstatement();/ X6 u# _$ w3 B# U; m; E& s
rs=stmt.executequery(sqld);
# e! d% m) o3 V- l9 _# ?2 A/ Bwhile(rs.next())+ i5 h) Z0 c+ R! F. }9 }6 I
{
. Y  j* G, {& I% H6 j  N3 n) k%>
! x0 h. q- j/ @, ]/ s. q5 h  <option name="<%=rs.getstring("fno")%>"><%=rs.getstring("fname")%></option>/ q  ?# k; [' u" x7 h
<%
/ N2 c& S$ E8 k$ X3 b4 ?}
4 L; G  g9 r# I8 P' d* H%>
, ]( M  J+ K0 B7 z+ G  </select>
! D  V- s% Y1 I/ f& R</td>" h. y0 i2 ?& e, J6 ?2 @
    <td>2 k. c  P% W! F
  <select multiple id=city style="width:150;height:200" class="bgc"># ?. C( u# G+ B. b5 }
  </select>
4 U# F/ n' P1 t+ S# C- n2 |( _9 f</td>
, y* N4 d; v# J% y2 n) x    <td nowrap align="center" class="bgc">
. D) Z* g3 d9 W* _* g  <input type="button"  value="<<" class="buttons">+ S8 I" p/ N5 |+ M4 X( O4 L
  <input type="button"  value=">>" class="buttons">5 W3 a1 o( G5 u+ p8 q5 k
</td>: g  F9 `4 _$ v8 u
    <td>
+ ?# j' i, B3 n( K1 H  <select multiple size="10" name="list2" style="width:150;height:200" class="bgc">: _+ b' t& F. c( g
  </select>/ m, S6 U5 `- S8 B
</td>
* u+ a7 X0 W# `# l) v( Q  </tr>
9 i; z! J- p- M% b% K3 g. f    <tr class="bgc"> * [% t) @( x4 y
    <td colspan="4" align="center"><input type="button" name="button1" class="buttons" value="选好了!" ></td>
8 Q. ~9 f: H  I( ~" I; t/ x- s  </tr>
5 x1 @+ r. k) i7 o" W/ z+ M. Z</table>
8 c3 s. L) v7 M</form>% a) ?$ j( |( o
<script language="javascript">
' V: {) Q$ G& ?" J4 @//人名移动
8 S6 k3 c" R9 T) g& ifunction move(fbox, tbox) {
# ]  @) @# H# e8 Tvar arrfbox = new array();
% a# e* X2 R; k4 `3 d( pvar arrtbox = new array();
7 u. t* V' k4 r' @5 Z8 m  M2 |var arrlookup = new array();
( t+ o9 y# E5 c" B, ?var i;
4 U' T1 w* g9 i4 i3 a" [1 \for (i = 0; i < tbox.options.length; i++) {
! [4 V# x5 e2 i  U; Jarrlookup[tbox.options.text] = tbox.options.value;5 J0 F4 }; w/ m* N! k5 I
arrtbox = tbox.options.text;, i4 ?8 J, v9 e# w9 A, J2 K; p
}2 j! |  H" @8 |& A; `
var flength = 0;
- \' l: k* }9 {) m; \3 Z' ?var tlength = arrtbox.length;9 i" i( f5 z3 t! x  s( d1 I
for(i = 0; i < fbox.options.length; i++) {! Q- u- B  n- z, h
arrlookup[fbox.options.text] = fbox.options.value;. j8 w5 b8 E  ?
if (fbox.options.selected && fbox.options.value != "") {; ~5 y5 |& E2 l
arrtbox[tlength] = fbox.options.text;/ |7 D, e6 L" `, G) S& e6 T3 C
tlength++;" M& D% `4 s/ U8 k: q
}
: A# X& H* m5 _. gelse {
# c2 G, {# o& barrfbox[flength] = fbox.options.text;8 [' _1 Y% f6 ^" V" L
flength++;$ E3 e5 e! F; c5 A' E6 O2 k
   }, x" f& O9 F+ E5 [
}
( {4 B& I" u9 W% O8 O  i" p3 marrfbox.sort();. ~1 p; ~2 x7 |" e  ]
arrtbox.sort();' x& W8 Q6 M3 m1 m
* x; i+ E% U2 s1 M6 O5 d# N
fbox.length = 0;% d" L. Q! x, o8 Z: A0 ~$ ^
tbox.length = 0;
& }" C. z8 K, J, a, {7 xvar c;$ Y) x* ~6 M& P- L: {4 M
for(c = 0; c < arrfbox.length; c++) {/ J/ w1 X. N  k2 B) S
var no = new option();! |" f; ^% _# i$ C4 s
no.value = arrlookup[arrfbox[c]];
0 M$ Q8 w5 f$ A* A5 }) ]9 S" q' Cno.text = arrfbox[c];
" c; U9 z$ C9 o& B$ b% ~fbox[c] = no;" W; H9 L# n) _" ]4 T
}! d9 k" Q8 M- r/ A( q/ m
for(c = 0; c < arrtbox.length; c++) {: Q/ ^+ H" z+ C; }/ I& O* c
var no = new option();
$ i( r2 N# y! F& L" B9 Nno.value = arrlookup[arrtbox[c]];
9 o/ U" T& f0 V- ]# X/ h; Tno.text = arrtbox[c];
$ h/ B2 z4 m: {) p5 c* M7 ]" Atbox[c] = no;  R5 W; v7 B8 s& d
   }) j1 U" f2 l6 w1 N9 p
}
5 }% U' i" c, P6 d) d) I/ v" r</script>
2 t  |( D& r- |1 f( A</body>1 d, s) }$ d' \/ C* @. y4 @/ s





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