JAVA涂鸦
关于JAVA的点点滴滴
posts - 50,  comments - 689,  trackbacks - 0
基于网友的要求,我贴出在项目中用到的三级联动的代码:

jsp的代码:

<%@ page contentType = "text/html; charset=GBK"  import="java.util.*,com.wehave.hyerp.procurement.domain.Cgsqd"%>
<%@ taglib uri="struts-html" prefix="html" %>
<%@ taglib uri="struts-logic" prefix="logic" %>
<%@ taglib uri="struts-bean" prefix="bean" %>
<html>
    
<head>
        
<title> </title>
        
<link rel = "stylesheet" type = "text/css" href = "../css/olstyle.css">    
        
<SCRIPT type="text/javascript">
        var req;
        window.onload
=function(){
        }

        
        function Change_Select()
        
{
            var zhi
=document.getElementById('m_gykfwzlbb_lbbm').value;
            var url
="sqdSelect.go?method=getSkill&id="+zhi;
            
if(zhi=="0"){
                alert(
"请选择您要察看的信息");
                   
return;
            }
else{
                
if(window.XMLHttpRequest)
                
{
                    req
=new XMLHttpRequest();
                }
else if(window.ActiveXObject)
                
{
                    req
=new ActiveXObject("Microsoft.XMLHTTP");
                }

                
                
if(req)
                
{
                    req.open(
"GET",url,true);
                    req.onreadystatechange
=callback;
                    req.send(
null);
                }

            }

        }

        
        function Change_Select2()
        
{
            var zhi
=document.getElementById('m_lbbm_se').value;
            
//alert(zhi.substring(0,2));
            if(zhi=="0"){
                alert(
"请选择您要察看的信息");
                   
return;
               }

            
if(zhi.substring(0,2)=="WY"){
                var url
="sqdSelect.go?method=getSkill2&id="+zhi;
                
if(window.XMLHttpRequest)
                
{
                    req
=new XMLHttpRequest();
                }
else if(window.ActiveXObject)
                
{
                    req
=new ActiveXObject("Microsoft.XMLHTTP");
                }

                
                
if(req)
                
{
                    req.open(
"GET",url,true);
                    req.onreadystatechange
=callback2;
                    req.send(
null);
                }

            }
else{
                parent.topFram.location 
="sqdSelectAll.go?method=getlistAll&id="+zhi;
            }

        }

        
        function Change_Select3()
        
{
            var zhi
=document.getElementById('m_lbbm_th').value;
            
//alert(zhi.substring(0,2));
            if(zhi=="0"){
                alert(
"请选择您要察看的信息");
                   
return;
               }
else{
                parent.topFram.location 
="sqdSelectAll.go?method=getlistAll2&id="+zhi;
            }

        }

        
        function callback()
        
{
            
if(req.readyState == 4)
            
{
                
if(req.status == 200)
                
{
                    parseMessage();
                }
else{
                    alert(
"Not able to retrieve description"+req.statusText);
                }

            }

        }

        
        function callback2()
        
{
            
if(req.readyState == 4)
            
{
                
if(req.status == 200)
                
{
                    parseMessage2();
                }
else{
                    alert(
"Not able to retrieve description"+req.statusText);
                }

            }

        }

        
        function parseMessage()
        
{
            var xmlDoc
=req.responseXML.documentElement;
            var xSel
=xmlDoc.getElementsByTagName('select');
            var select_root
=document.getElementById('m_lbbm_se');
            select_root.options.length
=0;
            
            
for(var i=0;i<xSel.length;i++)
            
{
                var xValue
=xSel[i].childNodes[0].firstChild.nodeValue;
                var xText
=xSel[i].childNodes[1].firstChild.nodeValue;
                var option
=new Option(xText,xValue);
                
try{
                    select_root.add(option);
                }
catch(e){
                }

            }

        }

        
        function parseMessage2()
        
{
            var xmlDoc
=req.responseXML.documentElement;
            var xSel
=xmlDoc.getElementsByTagName('select');
            var select_root
=document.getElementById('m_lbbm_th');
            select_root.options.length
=0;
            
            
for(var i=0;i<xSel.length;i++)
            
{
                var xValue
=xSel[i].childNodes[0].firstChild.nodeValue;
                var xText
=xSel[i].childNodes[1].firstChild.nodeValue;
                var option
=new Option(xText,xValue);
                
try{
                    select_root.add(option);
                }
catch(e){
                }

            }

        }

        
        
        
    
</SCRIPT>
    
</head>

<body bgcolor = "#C8D0D4">
    
<html:form action="/cgsqdNewAction.go"> 
    
<TABLE class = "cbToolbar" id = "idToolbar" cellpadding = '0' cellspacing = '0'>
        
<TR align = "left" valign = "top" >
            
<TD  NOWRAP>&nbsp;物质类别:&nbsp;&nbsp;
                
<html:select property="m_gykfwzlbb_lbbm" onchange="Change_Select()">
                    
<html:option value="0">请选择</html:option>
                       
<html:options collection="LbfList" property="m_gykfwzlbb_lbbm" labelProperty="m_gykfwzlbb_lbmc"/>
                
</html:select> &nbsp;
                
<html:select property="m_lbbm_se" styleId="m_lbbm_se" onchange="Change_Select2()">
                    
<html:option value="0">&nbsp;&nbsp;&nbsp;</html:option>
                
</html:select>
                
<html:select property="m_lbbm_th" styleId="m_lbbm_th" onchange="Change_Select3()">
                    
<html:option value="0">&nbsp;&nbsp;&nbsp;</html:option>
                
</html:select>
                
            
</TD>
            
        
</tr>
    
</table>
     
</html:form>
</body>
</html>

action中的代码:
/**
     * 
     * 查询物质编码类别列表操作(一级)
     * 
*/

    
public ActionForward doSelectWzlb(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest req,
            HttpServletResponse res) 
{
        HttpSession session 
= req.getSession();
        UserSession userSession 
=
            (UserSession) session.getAttribute(
"userSession");
        permission.setUserID(userSession.getUserId());
        permission.setUserName(userSession.getUserName());
        permission.setModuleName(
"m_cgsqd");
        permission.setActionStr(
"'m_select'");
        
if(permissionService.checkUserPermission(permission)){
            
int updateSign=0;
            
try{
                cgsqdService.updateCgsqwzhzb_sqsl();
                updateSign
=1;
            }
catch(Exception e){
                e.printStackTrace();
                updateSign
=0;
            }

            
if(updateSign==1){
                List LbfList
=cgsqdService.treeListWzlb2();
                req.setAttribute(
"LbfList",LbfList);
                
return mapping.findForward("tools");
            }
else{
                
return null;
            }

        }
else{
            
return mapping.findForward("failure");
        }

    }

    
    
/**
     * 
     * 查询物质编码类别列表操作(二级)
     * 
*/

    
public ActionForward doSelectWzlb1(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest req,
            HttpServletResponse res) 
{
        HttpSession session 
= req.getSession();
        UserSession userSession 
=
            (UserSession) session.getAttribute(
"userSession");
        permission.setUserID(userSession.getUserId());
        permission.setUserName(userSession.getUserName());
        permission.setModuleName(
"m_cgsqd");
        permission.setActionStr(
"'m_select'");
        
if(permissionService.checkUserPermission(permission)){
            
int updateSign=0;
            
try{
                cgsqdService.updateCgsqwzhzb_sqsl();
                updateSign
=1;
            }
catch(Exception e){
                e.printStackTrace();
                updateSign
=0;
            }

            
if(updateSign==1){
                List LbfList
=cgsqdService.treeListWzlb2();
                req.setAttribute(
"LbfList",LbfList);
                
return mapping.findForward("tools1");
            }
else{
                
return null;
            }

        }
else{
            
return mapping.findForward("failure");
        }

    }

    
    
/**
     * 
     * 查询物质编码类别列表操作(三级)
     * 
*/

    
public ActionForward getSkill(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest req,
            HttpServletResponse res) 
{
        HttpSession session 
= req.getSession();
        UserSession userSession 
=
            (UserSession) session.getAttribute(
"userSession");
        permission.setUserID(userSession.getUserId());
        permission.setUserName(userSession.getUserName());
        permission.setModuleName(
"m_cgsqd");
        permission.setActionStr(
"'m_select'");
        
if(permissionService.checkUserPermission(permission)){
            String id
=req.getParameter("id");
            
            res.setContentType(
"text/xml;charset=GBK");
            res.setHeader(
"Cache-Control","no-cache");
            String xml_start
="<?xml version=\"1.0\" encoding=\"GBK\"?>";
            xml_start
+="<selects>";
            String xml_end
="</selects>";
            String xml
="<select><value>0</value><text>请选择</text></select>";
            String m_lbbm_se
="";
            String m_lbmc_se
="";
            List LbfList
=null;
            
if(id.equals("WY")){
                LbfList
=cgsqdService.treeListWzlb3_2(id);
            }
else{
                LbfList
=cgsqdService.treeListWzlb3_1(id);    
            }

            Iterator it
=LbfList.iterator();
            
while(it.hasNext()){
                Cgsqd cgsqd
=(Cgsqd)it.next();
                m_lbbm_se
=cgsqd.getM_gykfwzlbb_lbbm();
                m_lbmc_se
=cgsqd.getM_gykfwzlbb_lbmc();
                xml 
+="<select><value>"+m_lbbm_se+"</value><text>"+m_lbmc_se+"</text></select>";
            }

            String last_xml
=xml_start+xml+xml_end;
            logger.debug(
"XML是:"+last_xml);
            
try {
                res.getWriter().write(last_xml);
            }
 catch (IOException e) {
                e.printStackTrace();
            }

            
return null;
        }
else{
            
return null;
        }

    }

主要代码就这些,去掉一些权限大代码,真正的代码并不多。
posted on 2006-08-09 15:21 千山鸟飞绝 阅读(12881) 评论(10)  编辑  收藏 所属分类: Ajax

FeedBack:
# re: Ajax实现三级联动下拉框
2006-08-26 19:28 | yyw_1023
高手就是高手!佩服佩服!!!!!!!!!!1  回复  更多评论
  
# re: Ajax实现三级联动下拉框
2006-09-15 11:18 | zch
我按照你的方法做了下,在onchange的时候,报了一个这样的错误~!
no action instance /SeclectAll.do ...
配置
<action path="/SelectAll"
type="com.hdtq.main.SelectAction"
scope="request">
<forward name="success" path="/MyLogin.jsp"/>
................
请问这是怎么回事呢???
..
var url="/teach/SelectAll.do?method=g1&id="+zhi;
....  回复  更多评论
  
# re: Ajax实现三级联动下拉框
2007-06-12 11:37 | 啊啊
能不能用三维数组实现三级联动阿?  回复  更多评论
  
# re: Ajax实现三级联动下拉框
2007-06-12 14:20 | 千山鸟飞绝
@啊啊
可以参考这篇文章:
javascript实现二级联动下拉框
http://www.blogjava.net/rickhunter/articles/64607.html  回复  更多评论
  
# re: Ajax实现三级联动下拉框[未登录]
2007-09-14 17:05 | liu
我这里报错,是Not able to retrieve descriptionUnkown  回复  更多评论
  
# re: Ajax实现三级联动下拉框[未登录]
2008-05-09 10:07 | 七七
支持一下,借用啦。  回复  更多评论
  
# re: Ajax实现三级联动下拉框[未登录]
2008-10-25 12:52 | 红狼
请问你的UserSession是从哪得到的。能把详细类创建写一下吗?Action怎么与页面交互的?急求回答75974082@qq.com  回复  更多评论
  
# re: Ajax实现三级联动下拉框
2008-10-28 22:40 | wa
javax.servlet.ServletException: javax.servlet.jsp.JspException: Cannot find bean under name LbfList
为什么会这样的啊?????????????  回复  更多评论
  
# re: Ajax实现三级联动下拉框
2008-12-28 20:25 | 盗版
原创? 绝对的“原创”!!!!!!!!!!!  回复  更多评论
  
# re: Ajax实现三级联动下拉框
2011-10-13 09:17 | tao
厉害!我是新手。还得向您请教个问题。帮忙到我的看看我这段代码。。。
http://blog.csdn.net/myjsj_2006/article/details/6868701
加载特别慢,能不能快点
  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航:
 
正在阅读:



<2019年9月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

常用链接

留言簿(35)

随笔档案

文章分类

文章档案

好友的blog

我的其他blog

老婆的Blog

搜索

  •  

积分与排名

  • 积分 - 750249
  • 排名 - 50

最新评论

阅读排行榜

评论排行榜