下拉框动态对应问题
我想做一个部门和员工的动态对应的两个下拉框,选中第一个部门,第二个下拉框里所有属于这个部门的员工全部显示出来,要不刷新的,最好给出代码 ,谢谢!
------解决方案--------------------先全部从数据库取出存入数组
<script LANGUAGE=JavaScript>
<!--
var name=new Array();
<%
for(int i=0;i <parrStsUserList.size();i++)
{
pobjCp = (Cp)parrStsUserList.get(i);
out.println( "name[ "+i+ "]=new Array( ' "+pobjCp.getCpID()+ " ', ' "+pobjCp.getCpname()+ " ', ' "+pobjCp.getCpuseid()+ " '); ");
}
%>
-->
</script>
getCpID是ID,getCpname是名字(王小二),getCpuseid是他的部门ID
<script LANGUAGE=JavaScript>
<!--
function onChange(dep)
{
ln = Form.name.options.length;
while(ln--) {
Form.name.options[ln] = null;//清空员工姓名的select的值
}
for(i=0;i <name.length;i++)
{
if(name[i][2]==dep)//如果name[i][2]部门ID等于dep时
{
Form.name.add(new Option(name[i][0],name[i][1]));//就给员工姓名的select增加一个option,name[i][0],name[i][1]这两个的位置你自己试试
}
}
}
-->
</script>
<form name=Form type=post>
<select name= "dep " onchange= "onChange(this.value) "> //当值改变时就执行函数
<option value= "部门ID "> 部门名 </option> //中间的option是从数据库读出生成的
</select>
<select name= "name ">
<option value= "0 "> 请先选择部门 </option>
</select>
</form>
------解决方案--------------------这中问题比较简单,最常见的有日期下拉,中国省份/城市/县下拉联动,我给你一个列子,希望对你有所帮助:
在JavaScript中写如下,我用的是struts,别的原理一样都是先查出来放到多维数组里,然后通过下拉,拉筛选出后面下拉框要显示的数组,予以显示
===================================================================================
//省份-市级联动Bean取得
<bean:define id= "placeKb12List " name= "outputBean " property= "placeKb12Bean " type= "java.util.ArrayList " />
<bean:size id= "size " name= "placeKb12List " />
var arrPlaceKb12List=new Array( <bean:write name= "size "/> );
//省份-市级-县市联动Bean取得
<bean:define id= "placeKb123List " name= "outputBean " property= "placeKb123Bean " type= "java.util.ArrayList " />
<bean:size id= "size1 " name= "placeKb123List " />
var arrPlaceKb123List=new Array( <bean:write name= "size1 "/> );
var i; //循环使用
//省份-市级联动2维数组
for (i=0; i <arrPlaceKb12List.length; i++) {
arrPlaceKb12List[i]=new Array(1);
}
<logic:iterate id= "item " name= "placeKb12List " indexId= "index ">
arrPlaceKb12List[ <bean:write name= "index " /> ][0]= " <bean:write name= "item " property= "placeKb1 " /> ";
arrPlaceKb12List[ <bean:write name= "index " /> ][1]= " <bean:write name= "item " property= "placeKb2 " /> ";
arrPlaceKb12List[ <bean:write name= "index " /> ][2]= " <bean:write name= "item " property= "placeKb2Name " /> ";
</logic:iterate>
//省份-市级-县市联动2维数组
for (i=0; i <arrPlaceKb123List.length; i++) {
arrPlaceKb123List[i]=new Array(1);
}
<logic:iterate id= "item1 " name= "placeKb123List " indexId= "index ">
arrPlaceKb123List[ <bean:write name= "index " /> ][0]= " <bean:write name= "item1 " property= "placeKb1 " /&g