javaScript实现表单联动

来源:互联网 发布:java常用算法 编辑:程序博客网 时间:2024/05/21 04:23

   在接受到action的请求后,在后台首先查出连个集合,比如  所有的部门,所有的人员

   List<UserInfoVO> users = userInfoFacede.getAllUsers();
   request.setAttribute("users", users);   //把值传给表单


   List<DeptVO> dept = userInfoFacede.getAllDeptName();
   request.setAttribute("dept", dept);

 

 

   在jsp页面

写在<script type="text/javascript"></script>

 

   var onecount;
    onecount=0;
    subcat=new Array();
         
          <logic:iterate id="small" name="users" indexId="index">
                  subcat[${index}] = new Array("${small.username}","${small.deptid}","${small.userid}");
          </logic:iterate>
    onecount=${index+1};
   
    function changelocation(locationid){
          document.forms[0].areaid.options[0]=new Option("-请选择-","");
                document.forms[0].areaid.length = 1;

                var locationid=locationid;
                var i;
          for (i=0;i < onecount; i++) {
            if (subcat[i][1] == locationid)
             {
                  document.forms[0].areaid.options[document.forms[0].areaid.length] = new Option(subcat[i][0], subcat[i][2]);
                  //取到部门的ID和姓名  分别和数组对应
             }        
          }
           
    }

 

分别与之对应

      <td align="right" bgcolor="#eeeeee">
         所属部门:
        </td>
        <td>
           <select name="rootid" id="rootid"
          onchange="changelocation(document.forms[0].rootid.options[document.forms[0].rootid.selectedIndex].value)">
          <option value="" selected>
           --请选择--
          </option>
          <logic:iterate id="big" name="dept">
           <option value="${big.pid }">
            ${big.deptname }
           </option>
          </logic:iterate>
         </select>
        </td>

 

         <td width="15%" align="right" bgcolor="#eeeeee">
         任务负责人:
        </td>
        <td width="30%">
         <select name="areaid" id="areaid">
          <option value="" selected>
           --请选择--
          </option>
         </select>
        </td>

 

 

这样就可以实现选择任意一个部门就可以得到部门的所有成员了~~~~~~~~~