Board logo

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

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

<%@ page import="java.util.date,yava.fileapp.*,java.sql.*;"
& ]2 Z% }5 p) A5 }! |         c pageencoding="gb2312"
* i) v1 }) |$ L8 Q" N9 u$ u! G. C%>2 A, A+ [9 X0 h. w8 L7 ~
<style>5 K# M) `" j% X& Q
.f9{ font-size:9pt; }  q  b& S/ ^1 C- A
.bgc{ background-color:#aecaf9; color: #0033ff }
- w3 W6 |6 o' B- Z: i.buttons{font-family:arial; font-size:13px; font-weight:bold; background-color:#6796e4; color:white; border-top: solid 2px #aacafb;
0 D, ~* r8 {0 t( D, S- v  border-bottom: solid 1px #4e7dc1;
2 d! M" f. f# p  border-left: solid 1px #aecaf9;" i" r: r1 ]  ]2 w- [
  border-right: solid 1px #5679bd;$ ?* v2 T% {+ @3 i
  padding:1px;
7 ^4 p1 D9 Y2 P& O2 L  margin:0px;}
+ G- a$ R) }+ [</style>
0 r5 m, ?, k4 q/ H<script language="javascript">7 S- Y0 v# P: F8 ^- }
<!--/ T, a& T0 r9 n4 j5 Q
function rv()
$ W7 n7 ?% _7 p; [% i{8 V3 j1 z) m" G. [, }5 L) L
  var val="";
2 y4 D5 u7 }4 G  for(i=0;i<combo_box.list2.length;i++){' O5 N& B; Y" L  i
   val+=","+combo_box.list2.value;. w. a$ u5 k0 ~7 T
  }
% G* S* p  o) d  a2 k6 A  if(val.charat(0)==","){
% I+ G. S3 U& v5 M   val=val.substr(1,val.length);
' C' @5 i2 L1 h9 r; L  }
  W8 U# m( j+ O4 D; X$ v  opener.form1.frecname.value=val;, M+ o/ e' D1 u0 P  b$ K- n8 k: L
  self.close();
' r0 E# T  T4 W* [4 X( J}
: ?. I* n" o0 d- f//-->
; C6 G- ?4 j; R5 B* M( K* p</script>
/ _# ?: L+ A8 V2 X<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
: t+ l0 _. O9 `( _" v<jsp:usebean id="user" scope="session" class="yava.fileapp.userbean" />
7 p# Q0 z/ a2 {+ x<%3 Y6 Z, ^) S" C; \1 k7 A7 l0 a( @
  cdatasource ds=new cdatasource();  //数据联结bean实例
4 u! A3 n+ Q' B3 V4 K  z$ K  java.sql.connection conn=ds.getconnection();
; I: s8 V: x2 O  java.sql.statement stmt=null;
$ q# I- p& C; ~  M4 j9 |2 S  java.sql.resultset rs=null;
" w" I' t; t5 I  cdatacheck dc=new cdatacheck();: `' b( p! Q0 D; a4 k
%>
* A1 L$ N! D& I! e# `<%
: {( P; t5 O" b( `+ Wstring 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";, ^( F- k& m0 ~1 k- ^! `8 w
stmt=conn.createstatement();# A7 |+ q5 e5 `
rs=stmt.executequery(sqlu);
4 c5 d( h  H6 p- l/ f7 t%># o0 z; X3 e2 b5 T: v. \
<script language='javascript'>& l* }" v  a! `/ ]
arr = new array();
# X  f. e9 ^  U<%  int temp=0;$ u0 R  D* ]0 b- N% ^% q# a5 u
while(rs.next())
! E% q& h/ K1 @' O" T% f8 d{
: h$ H$ C/ l" e, D4 v' A2 d7 G2 B%>
! M* M: y' p1 u- U! _. \, w2 _arr[<%=temp%>]=new array("<%=rs.getstring("fname")%>","<%=rs.getstring("fdept")%>");/ S, B: T+ d' m5 m8 `
<%
7 k& C" y& X9 d9 a- Z/ O, _1 Ftemp = temp + 1;
0 Z3 H$ v3 E, i6 Z4 o# p; |}
6 o/ ?0 U; M; j% o5 |! `%>
& ]- |! `  X. G7 Otemp=<%=temp%>;: j" [5 _' g; e0 L
function changelocation(id){
9 w: E+ v8 l5 x1 I! L6 p2 ]document.combo_box.city.length=0; //初始化第2级菜单的长度,下标从0开始
, W( Z/ w, v( I1 l% ^3 h- Bvar i = 0; 7 P% N* W/ ^9 o4 N/ x
document.combo_box.city.options[0]=new option('-------',''); 9 E! X2 O# {# j
for(i=0;i<temp;i++){ 1 V5 I; A% Q, q, r! L; U* f- O  x
if(arr[1]==id){//如果相等,证明在第2级里面有输入第1级组织的子集,arr[总数目][部门]
' E* Y. h6 M& i9 h6 Pdocument.combo_box.city.options[document.combo_box.city.length] = new option(arr[0], arr[0]); 3 ~4 D8 |9 p6 m
}
1 f' x& h9 y: j}
4 [9 |8 K) K' t6 {3 n5 a}
9 _8 s" ^/ b9 s</script>
+ d6 }* T" k) G8 U, L: O
0 d, f' f  D; g5 b1 g7 [<form name="combo_box">
9 O: q3 |6 c8 x0 l  D* D' i4 Y<table border="0" cellspacing="0" cellpadding="0" height="210" width="59">0 k1 t! D8 k  A4 ?4 X
  <tr height="24">
) j. d" E% x+ D' l; v  n    <td bgcolor="#336699" class="buttons" align=center><font color=#ffffff>部门选择</font></td>
* J- \! T$ h' t4 ~    <td bgcolor="#336699" class="buttons" align=center><font color=#ffffff>人名选择</font></td>
- r$ O: y5 Z4 Y* a1 O6 p" G. j    <td bgcolor="#336699" class="buttons" align=center nowrap><font color=#ffffff>添加/移除</font></td>
9 I; _. s/ i/ E0 z# n/ y2 V) L    <td bgcolor="#336699" class="buttons" align=center><font color=#ffffff>最终人</font></td>
/ H1 i6 i' W& s  X+ q  </tr>
0 P! {* F6 z4 x  s7 a6 j9 b$ Y  <tr> 0 ~- M) o7 b/ T
    <td>
2 [# r. b% {0 N* d  <select multiple id=prov name="prov"    style="width:150;height:200" class="bgc">
  U5 u9 y+ N3 H; |0 u  <option value="0">请选择部门 ---></option># _# o/ A' B& @, G" ~
<%/ M' I8 v! Y# n5 F9 p0 t2 n7 b$ N6 d
    string sqld="select * from tdept";2 {: a8 u0 n, O- D) v- M% e
stmt=conn.createstatement();
9 V( J9 i, p+ j  yrs=stmt.executequery(sqld);
  I4 j. f$ a6 v* A/ ywhile(rs.next())
' U& C9 y; W" t, y) V{, ~1 L# r: U& Y! x$ J9 s. b( W: W
%>5 E+ F* {  y7 {5 v: y! `% r( z
  <option name="<%=rs.getstring("fno")%>"><%=rs.getstring("fname")%></option>/ h4 Q: K$ d: v1 _. l' |  ~% [
<%8 b+ L+ [: ~; Y
}
+ J& v9 n  @- G' l4 e' V" b& [%>
! y: x3 G4 D' E5 S. W5 M, R8 z  </select>
/ y* V4 Z$ B2 x; t</td>7 w0 i4 `) t3 i2 D) i0 t  T3 j
    <td>% v, b) s9 S0 n+ C
  <select multiple id=city style="width:150;height:200" class="bgc">* i" V% s/ U5 a6 Q. H: _! P
  </select>( |$ X' T0 k( b0 t
</td>
6 V) X# N' r) V% G6 w, l" T    <td nowrap align="center" class="bgc">
( C- C1 k# I/ `+ w$ e4 k' O( d  <input type="button"  value="<<" class="buttons">9 n' {! ?3 P- D* C6 |' t" u( v
  <input type="button"  value=">>" class="buttons">
; s2 N' U( |( e7 Q</td>
! i, m" A. ?& g3 p6 ^' h; U* P( F    <td>
! O1 Y5 D0 \2 ?3 ?  <select multiple size="10" name="list2" style="width:150;height:200" class="bgc">
% d4 H9 @. Q" _- ^( T  </select>+ X- K3 Y; u4 Q- j4 I
</td>
+ R) J) W3 t# d* h/ P: S5 t: A  </tr>
: O1 {+ F+ v  f! m, _' {    <tr class="bgc"> & o% @/ m; ]+ [/ {5 u% c
    <td colspan="4" align="center"><input type="button" name="button1" class="buttons" value="选好了!" ></td>" V3 \* b# @' d) N
  </tr>
8 U! ]0 ^3 y2 ?</table>) {- g1 F" R+ q$ B
</form>- ?& S5 M2 g" `" ~
<script language="javascript">
5 n8 f! \. y6 G//人名移动
& \! D; W' c2 ~1 T2 b8 c" gfunction move(fbox, tbox) {' R: O, L7 c" Q  s: p7 W6 f
var arrfbox = new array();
) a; n  a% e' lvar arrtbox = new array();
+ T) O. |$ ^  P! svar arrlookup = new array();
  r2 D. i+ d, V  C6 v: Ovar i;
; o/ k9 |: G6 tfor (i = 0; i < tbox.options.length; i++) {
& v* t" |' f) J- A1 Oarrlookup[tbox.options.text] = tbox.options.value;, G! l. p1 n! \
arrtbox = tbox.options.text;
- L. J* |8 ~2 {' L' _6 B" D}
# K, ^9 ~3 Z8 dvar flength = 0;
, f  m, f, \2 J0 f& ?" O0 j: T5 Rvar tlength = arrtbox.length;' r4 f0 P% L3 m5 F5 p0 k: z
for(i = 0; i < fbox.options.length; i++) {1 d6 M5 p- V4 F, _  E3 t# {2 U, y
arrlookup[fbox.options.text] = fbox.options.value;
9 u; q. f& V* `if (fbox.options.selected && fbox.options.value != "") {
- B) L7 o4 n2 O9 w- u5 barrtbox[tlength] = fbox.options.text;& g1 W7 S  W9 s- M/ {
tlength++;
/ Z- X- d6 r7 n}+ p$ z1 [# \+ t
else {
1 X* d: F. A+ s+ Z, F2 xarrfbox[flength] = fbox.options.text;
9 o: v# R% k5 S: l. S5 T& Vflength++;
6 A1 |0 v, W" B/ x; h) @   }4 r' @3 m( F. Z/ l+ Z# J
}: q3 ]8 n) w- F  D* s% L  V1 R
arrfbox.sort();
% p6 r! p) ?8 G: \9 s" @arrtbox.sort();5 f. N( K5 n1 v- _9 {. y: Q
" U8 M4 w9 a$ F6 I6 [
fbox.length = 0;
2 w  [. U2 B$ A% b$ q6 ]tbox.length = 0;" _6 h2 g, b) B
var c;
( A6 v$ r& J* z% pfor(c = 0; c < arrfbox.length; c++) {# z# R% K: [8 C) Z5 C2 C7 y; a, j
var no = new option();/ N% w( J$ Q$ ^+ c" M
no.value = arrlookup[arrfbox[c]];" Q* B' u) {  M2 ^& g6 d
no.text = arrfbox[c];
$ C# B- t4 \  g0 v4 X. H5 S$ S  d) dfbox[c] = no;9 f% J8 ^4 C% x- n, i/ R* Q
}
  A$ F9 k, v6 h5 p: a8 Y. V0 k; ffor(c = 0; c < arrtbox.length; c++) {
% l# Q. J9 X7 c5 @# }var no = new option();
7 W) _' C$ H7 r$ Fno.value = arrlookup[arrtbox[c]];
" j7 a# g3 |' e7 }8 B. gno.text = arrtbox[c];- R) \- i6 e9 ^8 `0 _% v; \0 b
tbox[c] = no;3 @; F( K& k' H2 W6 }7 |8 J
   }& i3 o# F; b0 b3 A# B
}
2 r/ n  W: i) Q' W% M5 i. W5 r</script>
! c* z$ A0 v8 P6 Q1 v' G</body>
6 Y5 X- }6 q6 Z7 [- Z9 e- n9 W





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