<陈市明摘自:http://www.blogjava.net/JPeanut>
在公司需求预测模块开发了无限级下拉菜单js版本
下载
1
使用注意一下几点:
<
br
>
2
<
OL
>
3
<
LI
>
selected 属性 : index(索引,必填) defalut(默认值,可选)
4
<
LI
>
array(a,b,c) : a(上级下拉的值域,b当前级下拉的值域,c当前级下拉值域对应的显示的字符串)
5
<
LI
>
调用方法:
6
<
pre
>
7
var loarbar = new bsteel_associate_list(
8
new Array(
<
----
下拉关联属性
9
new Array("ff.a",a_array), <----new Array("表单属性名",数组)
10
new Array("ff.b",b_array),
11
new Array("ff.c",c_array)
12
),
13
3, <----数量
14
'请选择' <----第一条选择的option的
15
);
16
</pre
>
17
<
LI
>
18
<
LI
>
19
</
OL
>
20
<
form
method
=post
action
=""
name
="ff"
>
21
<
select
name
="a"
index
="0"
></
select
>
22
<
select
name
="b"
index
="0"
></
select
>
23
<
select
name
="c"
index
="0"
></
select
>
24
<
hr
>
25
<
select
name
="a"
index
="1"
defalut
="1"
></
select
>
26
<
select
name
="b"
index
="1"
defalut
="12"
></
select
>
27
<
select
name
="c"
index
="1"
defalut
="121"
></
select
>
28
<
hr
>
29
<
select
name
="a"
index
="2"
></
select
>
30
<
select
name
="b"
index
="2"
></
select
>
31
<
select
name
="c"
index
="2"
></
select
>
32
<
hr
>
33
</
form
>
34
<
script
language
="javascript"
>
35
<!--
36
37
38
a_array
=
new
Array(
new
Array(
""
,
"
1
"
,
"
[1]
"
),
new
Array(
""
,
"
2
"
,
"
[2]
"
));
39
b_array
=
new
Array(
new
Array(
"
1
"
,
"
11
"
,
"
[11]
"
),
new
Array(
"
1
"
,
"
12
"
,
"
[12]
"
),
new
Array(
"
2
"
,
"
21
"
,
"
[21]
"
),
new
Array(
"
2
"
,
"
22
"
,
"
[22]
"
));
40
c_array
=
new
Array(
new
Array(
"
11
"
,
"
111
"
,
"
[111]
"
),
new
Array(
"
11
"
,
"
112
"
,
"
[112]
"
),
new
Array(
"
12
"
,
"
121
"
,
"
[121]
"
),
new
Array(
"
12
"
,
"
122
"
,
"
[122]
"
));
41
42
43
44
var
loarbar
=
new
bsteel_associate_list(
45
new
Array(
46
new
Array(
"
ff.a
"
,a_array),
47
new
Array(
"
ff.b
"
,b_array),
48
new
Array(
"
ff.c
"
,c_array)
49
),
50
3
,
51
'请选择'
52
);
53
loarbar.init();
54
55
56
57
/**/
/*
*
58
* 作者: 陈市明
59
* 作用:无限级关联下拉菜单
60
* 用法:
61
62
63
*/
64
function
bsteel_associate_list(lists,count,firstStr)
{
65
this
.count
=
count;
66
this
.init
=
onload_associate_list;
67
this
.onchange_associate_list
=
onchange_associate_list;
68
this
.remove_list_option
=
remove_list_option;
69
this
.update_input_name
=
update_input_name;
70
this
.static_associate_list
=
lists;
71
this
.count
=
count;
72
this
.firstStr
=
firstStr;
73
74
}
75
76
function
update_input_name(obj)
{
77
if
(obj.index
!=
"
undefined
"
)
{
78
return
obj.name
+
"
[
"
+
obj.index
+
"
]
"
;
79
}
else
{
80
return
obj.name;
81
}
82
}
83
84
85
function
onload_associate_list()
{
86
var
i,j;
87
var
x,y;
88
89
for
(x
=
0
,y
=
this
.count;x
<
y;x
++
)
{
90
for
(i
=
0
,j
=
this
.static_associate_list.length;i
<
j;i
++
)
{
91
eval(
this
.static_associate_list[i][
0
]
+
update_list_index(x,
this
.count)).options.add(
new
Option(
this
.firstStr,
""
));
92
93
}
94
95
}
96
97
//
初始化所有的表单 的 全局变量
98
99
for
(x
=
0
,y
=
this
.count;x
<
y;x
++
)
{
100
for
(i
=
0
,j
=
this
.static_associate_list.length;i
<
j;i
++
)
{
101
eval(
this
.static_associate_list[i][
0
]
+
update_list_index(x,
this
.count)).setAttribute(
"
static_associate_list
"
,
this
.static_associate_list);
102
eval(
this
.static_associate_list[i][
0
]
+
update_list_index(x,
this
.count)).setAttribute(
"
static_associate_count
"
,
this
.count);
103
eval(
this
.static_associate_list[i][
0
]
+
update_list_index(x,
this
.count)).setAttribute(
"
firstStr
"
,
this
.firstStr);
104
}
105
}
106
107
108
//
初始化所有的表单 的 数据
109
var
a_array
=
this
.static_associate_list[
0
][
1
];
110
111
for
(x
=
0
,y
=
this
.count;x
<
y;x
++
)
{
112
for
(i
=
0
,j
=
a_array.length;i
<
j;i
++
)
{
113
eval(
this
.static_associate_list[
0
][
0
]
+
update_list_index(x,
this
.count)).options.add(
new
Option(a_array[i][
2
],a_array[i][
1
]));
114
}
115
}
116
117
118
//
初始化所有的表单 添加 onchange 触发动作
119
for
(x
=
0
,y
=
this
.count;x
<
y;x
++
)
{
120
for
(i
=
0
,j
=
this
.static_associate_list.length
-
1
;i
<
j;i
++
)
{
121
eval(
this
.static_associate_list[i][
0
]
+
update_list_index(x,
this
.count)).onchange
=
onchange_associate_list;
122
}
123
}
124
125
//
初始化所有的表单 的 默认值
126
127
for
(x
=
0
,y
=
this
.count;x
<
y;x
++
)
{
128
for
(i
=
0
,j
=
this
.static_associate_list.length
-
1
;i
<
j;i
++
)
{
129
eval(
this
.static_associate_list[i][
0
]
+
update_list_index(x,
this
.count)).value
=
eval(
this
.static_associate_list[i][
0
]
+
update_list_index(x,
this
.count)).defalut;
130
eval(
this
.static_associate_list[i][
0
]
+
update_list_index(x,
this
.count)).onchange();
131
//
在没有默认值的情况下,选择第一列:请选择
132
if
(eval(
this
.static_associate_list[i][
0
]
+
update_list_index(x,
this
.count)).selectedIndex
<=
0
)
{
133
eval(
this
.static_associate_list[i][
0
]
+
update_list_index(x,
this
.count)).selectedIndex
=
0
;
134
}
135
}
136
}
137
138
eval(
this
.static_associate_list[i][
0
]
+
update_list_index(x,
this
.count)).value
=
eval(
this
.static_associate_list[i][
0
]
+
update_list_index(x,
this
.count)).defalut;
139
//
在没有默认值的情况下,选择第一列:请选择
140
if
(eval(
this
.static_associate_list[i][
0
]
+
update_list_index(x,
this
.count)).selectedIndex
<=
0
)
{
141
eval(
this
.static_associate_list[i][
0
]
+
update_list_index(x,
this
.count)).selectedIndex
=
0
;
142
}
143
}
144
145
146
147
148
}
149
150
function
onchange_associate_list()
{
151
remove_list_option(
this
);
152
var
nextObj;
153
var
array;
154
var
i,j;
155
for
(i
=
0
,j
=
this
.static_associate_list.length;i
<
j;i
++
)
{
156
if
((
this
.form.name
+
"
.
"
+
this
.name)
==
this
.static_associate_list[i][
0
])
{
157
nextObj
=
eval(
this
.static_associate_list[i
+
1
][
0
]
+
update_list_index(
this
.index,
this
.static_associate_count));
158
array
=
this
.static_associate_list[i
+
1
][
1
];
159
break
;
160
}
161
}
162
163
//
nextObj.options.add(new Option("请选择",""));
164
for
(i
=
0
,j
=
array.length;i
<
j;i
++
)
{
165
if
(
this
.value
==
array[i][
0
])
{
166
nextObj.options.add(
new
Option(array[i][
2
],array[i][
1
]));
167
}
168
}
169
}
170
171
function
remove_list_option(currentObj)
{
172
var
remove_flag
=
false
;
173
var
i,j;
174
for
(i
=
0
,j
=
currentObj.static_associate_list.length;i
<
j;i
++
)
{
175
if
(remove_flag)
{
176
remove_list_all_option(eval(currentObj.static_associate_list[i][
0
]
+
update_list_index(currentObj.index,currentObj.static_associate_count)));
177
remove_flag
=
true
;
178
}
179
if
((currentObj.form.name
+
"
.
"
+
currentObj.name)
==
currentObj.static_associate_list[i][
0
])
{
180
remove_flag
=
true
;
181
}
182
}
183
}
184
185
function
remove_list_all_option(listObj)
{
186
var
i,j;
187
for
(i
=
0
,j
=
listObj.length;i
<
j;i
++
)
{
188
listObj.options.remove(
0
);
189
}
190
listObj.options.add(
new
Option(listObj.firstStr,
""
));
191
}
192
193
function
update_list_index(index,count)
{
194
if
(index
==
"
undefined
"
||
count
==
"
undefined
"
||
count
+
""
==
"
1
"
)
{
195
return
""
;
196
}
else
{
197
return
"
[
"
+
index
+
"
]
"
;
198
}
199
}
200
201
//
-->
202
</
script
>