欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Ajax级联菜单实例代码

程序员文章站 2023-11-07 17:42:16
1.ajax.html复制代码 代码如下:

1.ajax.html

复制代码 代码如下:

<!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>
    <title>ajax</title>
<script type="text/javascript">
    function loadxmldoc(txt) {
        var xmlhttp;
        if (window.xmlhttprequest) {// code for ie7+, firefox, chrome, opera, safari
            xmlhttp = new xmlhttprequest();
        }
        else {// code for ie6, ie5
            xmlhttp = new activexobject("microsoft.xmlhttp");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readystate == 4 && xmlhttp.status == 200) {
                var citys = xmlhttp.responsetext.split(',');
                document.getelementbyid("citys").length = 1;
                for (i = 0; i < citys.length - 1; i++) {

                    document.getelementbyid("citys").add(new option(citys[i], citys[i]));
                }
                //                document.getelementbyid("mydiv").innerhtml = xmlhttp.responsetext;
            }
        }
        xmlhttp.open("get","ajax/getdata.aspx?pro="+txt, true);
        xmlhttp.send();
    }
</script>
</head>
<body>

<h2>ajax</h2>
    <select id="select1" onchange="loadxmldoc(this.value)">
        <option>请选择省份</option>
        <option value="1">江苏</option>
        <option value="2">上海</option>
    </select>
       <select id="citys">
        <option>请选择城市</option>
    </select>
<div id="mydiv"></div>

</body>
</html>


2.getdata.aspx.cs
复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;

public partial class ajax_getdata : system.web.ui.page
{
    protected void page_load(object sender, eventargs e)
    {
        string[] js=new string[]{"南京",  "苏州",  "常州" , "无锡" ,  "镇江"};
        string pro=request.querystring["pro"];
        if (pro == "1")
        {
            string temp = "";
            for (int i = 0; i < js.length; i++)
            {
                temp = temp + js[i];

                temp = temp + ",";

            }
            response.write(temp);
        }
        else
            response.write("");
    }
}