当前位置:首页 > ASP教程 > 应用技巧

一个强健 实用的asp+ajax二级联动菜单(有演示和附源程序打包下载)

一个强健 实用的ajax二级联动菜单(有演示和附源程序打包下载)

前些天在搞后台的时候要用到二级联动的菜单,到网上去找了半天也没找到满意的,不是这错就是那错,在选择的时候有时候不能返回.真是郁闷.
     后来就看到有人用ajax写了无限级分类(牛,呵呵,本人看不懂.).就想到我那个后台不也可以用ajax试试,虽然比用javascript的慢点,但我这个实用.强健..不会出错,也不会选择某个大类后再返回无选择状态小类为空的状态.呵呵.
文件目录:如图

主要代码如下:
index.asp
复制代码 代码如下:

<%@language="vbscript" codepage="936"%>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" c />
<title>ajax的二级联动by啊峰</title>
<script language="javascript" src="js.js"></script>
</head>
<body>
<h2><a href="http://yeahdown.com/">ajax的二级联动by啊峰</a></h2>
<!--#include file="iconn.asp"-->
<% 
set afeng = conn.execute("select bigclassid,bigclassname from bigclass")
%>
<form id="form1" name="form1" method="post" action="">
  <div id="bigclass" style="float:left">
  <select name="select" >
     <option value="0">选择一级分类</option>
      <%if not afeng.eof then
        do while not afeng.eof
            bigclassid= afeng("bigclassid")
            bigclassname = afeng("bigclassname")%>
            <option value="<%=bigclassid%>"><%=bigclassname%></option>
        <%afeng.movenext
        loop
    end if
    afeng.close
    set afeng = nothing
    conn.close
    set conn = nothing%>
  </select>
  </div>
  <div id="subclass"  style="float:left"><select name="select2">
    <option value="0">选择二级分类</option>
  </select>
  </div>
</form>
</body>
</html>

getsubcategory.asp文件:
复制代码 代码如下:

<%
response.charset="gb2312" '设置字符集 原默认为utf-8
bigclassid=safe(request.querystring("bigclassid")) '非法字符过滤
if bigclassid<>"" then
        set re=new regexp
        re.ignorecase=true
        re.global=false
        re.pattern = "^[0-9]{1,3}$"
        if not re.test(bigclassid) then 
            response.write "非法参数"
            response.end
        end if%>
<!--#include file="iconn.asp"-->
    <%on error resume next
    set p = conn.execute("select * from smallclass where bigclassid="&bigclassid&"")
    if err then
        err.clear
        response.write "查询出错"
        response.end 
    end if
    if not p.eof then
        html = "<select name='smallclassid'>"&vbnewline
        do while not p.eof
        html = html&"<option value='"&p("smallclassid")&"'>"&p("smallclassname")&"</option>"&vbnewline
        p.movenext
        loop
        html = html&"</select>"
    else
        html = "<select name='smallclassid'><option value='0' selected>暂无小类</option></select>"
    end if
    p.close
    set p = nothing
    conn.close
    set conn = nothing
    response.write html
    html = ""
end if
%>

iconn.asp文件: 
复制代码 代码如下:

<%
set conn=server.createobject("adodb.connection")
db = "$#yeahdown.com@$$$@@%%.asp"
c & server.mappath(db)
on error resume next
conn.open connstr
if err then
    err.clear
    response.write "<div id=""error"">连接数据库出错</div>"
    response.end
end if

function safe(str)
if isnull(str) then
     safe = ""
     exit function 
end if
str = replace(str,chr(0),"", 1, -1, 1)
str = replace(str, """", """, 1, -1, 1)
str = replace(str,"<","<", 1, -1, 1)
str = replace(str,">",">", 1, -1, 1) 
str = replace(str, "script", "script", 1, -1, 0)
str = replace(str, "script", "script", 1, -1, 0)
str = replace(str, "script", "script", 1, -1, 0)
str = replace(str, "script", "script", 1, -1, 1)
str = replace(str, "object", "object", 1, -1, 0)
str = replace(str, "object", "object", 1, -1, 0)
str = replace(str, "object", "object", 1, -1, 0)
str = replace(str, "object", "object", 1, -1, 1)
str = replace(str, "applet", "applet", 1, -1, 0)
str = replace(str, "applet", "applet", 1, -1, 0)
str = replace(str, "applet", "applet", 1, -1, 0)
str = replace(str, "applet", "applet", 1, -1, 1)
str = replace(str, "[", "[")
str = replace(str, "]", "]")
str = replace(str, """", "", 1, -1, 1)
str = replace(str, "=", "=", 1, -1, 1)
str = replace(str, "'", "''", 1, -1, 1)
str = replace(str, "select", "select", 1, -1, 1)
str = replace(str, "execute", "execute", 1, -1, 1)
str = replace(str, "exec", "exec", 1, -1, 1)
str = replace(str, "join", "join", 1, -1, 1)
str = replace(str, "union", "union", 1, -1, 1)
str = replace(str, "where", "where", 1, -1, 1)
str = replace(str, "insert", "insert", 1, -1, 1)
str = replace(str, "delete", "delete", 1, -1, 1)
str = replace(str, "update", "update", 1, -1, 1)
str = replace(str, "like", "like", 1, -1, 1)
str = replace(str, "drop", "drop", 1, -1, 1)
str = replace(str, "create", "create", 1, -1, 1)
str = replace(str, "rename", "rename", 1, -1, 1)
str = replace(str, "count", "count", 1, -1, 1)
str = replace(str, "chr", "chr", 1, -1, 1)
str = replace(str, "mid", "mid", 1, -1, 1)
str = replace(str, "truncate", "truncate", 1, -1, 1)
str = replace(str, "nchar", "nchar", 1, -1, 1)
str = replace(str, "char", "char", 1, -1, 1)
str = replace(str, "alter", "alter", 1, -1, 1)
str = replace(str, "cast", "cast", 1, -1, 1)
str = replace(str, "exists", "exists", 1, -1, 1)
str = replace(str,chr(13),"<br>", 1, -1, 1)
safe = replace(str,"'","''", 1, -1, 1)
end function
%>

最后一个js.js 
复制代码 代码如下:

function createxmlhttp()
{
    xmlhttpobj = false;
    try{//创建对象,一个一个的试,哎,要是能统一标准都好。。
        xmlhttpobj = new xmlhttprequest;
    }catch(e){
        try{
            xmlhttpobj=new activexobject("msxml2.xmlhttp");
        }catch(e2){
            try{
                xmlhttpobj=new activexobject("microsoft.xmlhttp");
            }catch(e3){
                xmlhttpobj = false;
            }
        }
    }
    return xmlhttpobj; 
}
function getsubcategory(bigclassid){
    if(bigclassid==0){
        document.getelementbyid("subclass").innerhtml="<select name='smallclassid'><option value='0' selected>选择二级分类</option></select>";
        return;
    };
    var xmlhttpobj = createxmlhttp();
    if(xmlhttpobj){//如果创建对象xmlhttpobj成功
        xmlhttpobj.open('get',"getsubcategory.asp?bigclassid="+bigclassid+"&number="+math.random(),true);//get方法 加个随机数。
        xmlhttpobj.send(null);
        xmlhttpobj.onreadystatechange=function(){//客户端监控函数
            if(xmlhttpobj.readystate==4){//服务器处理请求完成
                if(xmlhttpobj.status==200){
                    //alert('ok');
                    var html = xmlhttpobj.responsetext;//获得返回值
                    document.getelementbyid("subclass").innerhtml=html;
                }else{
                    document.getelementbyid("subclass").innerhtml="对不起,您请求的页面有问题...";
                }
            }else{
                document.getelementbyid("subclass").innerhtml="加载中,请梢候...";//服务器处理中
            }
        }    
    }
}

打包文件下载

【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!

相关教程推荐

其他课程推荐