select下拉列表动态显示选择公司及部门信息——级联

本文介绍了一种在Java中为下拉选择框填充数据的方法,包括如何从公司和部门类中读取数据,并根据当前选中的项来设置默认选项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//=================公司类方法===================//
public String getDrop(String compid,String dataid) throws Exception{
      if(str.IsEmpty(dataid) )
        dataid="AAA";
      Compid  com[]=this.readAll(compid,dataid);
      StringBuffer bf = new StringBuffer();
      if(com.length > 0){
           for(int i=0;i<com.length ;i++){
             if(dataid.equals(com[i].getBh()) )
               bf.append("<option value=" + com[i].getID()  + " selected >" + com[i].getName() + "</option>") ;
             else
               bf.append("<option value=" + com[i].getID()  + ">" + com[i].getName() + "</option>") ;
           }
         }
    return bf.toString();
    }
 //=================部门类方法同上===================//

//====================页面调用=====================//

   Compid com = new Compid();
   Deptid dep = new Deptid();
   Deptid [] depAll=bm.readAll("","");

          <select name="deptid" style="width:120px">
           <option value="">所有公司
            <%=com.getDrop(compid,deptid)%>
          </select>

          <select name="deptid" style="width:120px">
           <option value="">所有部门
            <%=dep.getDrop(compid,deptid)%>
          </select>




<script language=javascript>

 var bm= new Array();
 <%if(depAll.length>0){
     for(int i=0;i<depAll.length;i++){%>
     bm[<%=i%>] = new Array("<%=depAll[i].getCom()%>","<%=depAll[i].getID()%>","<%=depAll[i].getName()%>");
  <%}}%>
 function setBm(){
  document.form_com_dep.deptid.length = 0;
  document.form_com_dep.deptid.options[document.form_com_dep.deptid.length] = new Option("所有部门","");
  var comp = document.form_com_dep.compid.value ;
  var i=0;
  for (i=0;i < <%=depAll.length%> ; i++)  {
      if (bm[i][0] == comp ) {
         document.form_com_dep.deptid.options[document.form_com_dep.deptid.length] = new Option(bm[i][2], bm[i][1]);
             }       
     }
 }
</script> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值