码迷,mamicode.com
首页 > Web开发 > 详细

【2017-6-7】AJAX异步刷新 省市区 三级联动

时间:2017-06-08 17:56:52      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:send   option   blog   function   pen   color   ges   联动   数据   

这里调用的是jquery1.7.1

需要数据库的支持

下面具体看代码

 

 

前面界面只需要三个下拉列表就OK

<form id="form1" runat="server">
        <div>
            <select id="st1">
                <option value="null">加载中...</option>
            </select>
            <select id="st2">
                <option value="null">加载中...</option>
            </select>
            <select id="st3">
                <option value="null">加载中...</option>
            </select>
        </div>
    </form>

 

LINQ to SQL类

技术分享

 

一般处理程序

public void ProcessRequest(HttpContext context)
    {
        StringBuilder str = new StringBuilder();
        str.Append("[");
        string s = context.Request["ccode"];
        using (ChinaStatesDataContext con = new ChinaStatesDataContext())
        {
            int count = 0;
            List<ChinaStates> clist = con.ChinaStates.Where(r => r.ParentAreaCode == s).ToList();
            foreach (ChinaStates c in clist)
            {
                if (count > 0)
                {
                    str.Append(",");
                }
                str.Append("{\"code\":\"" + c.AreaCode + "\",\"name\":\"" + c.AreaName + "\"}");
                count++;
            }
        }
        str.Append("]");
        context.Response.Write(str);
        context.Response.End();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

 

 

外部JS代码,一定记得引用

insert("1");

    function insert(a) {
        if (a == "1") {
            $.ajax({
                url:"Handler.ashx",
                data: {"ccode":"0001"},
                type: "post",
                dataType: "json",
                success: function (msg) {
                    $("#st1").empty();
                    for(m in msg)
                    {
                        var str = "<option value=‘" + msg[m].code + "‘>" + msg[m].name + "</option>";
                        $("#st1").append(str);
                    }
                },
                error: function () {
                    alert("a");
                },
                beforeSend: function () {
                    $("#st1").html(‘‘);
                    $("#st1").append("<option value=‘null‘>加载中...</option>");
                },
                complete: function () {
                    insert("2");
                }
            });
        }
        if (a == "2") {
            $.ajax({
                url: "Handler.ashx",
                data: { "ccode": $("#st1").val() },
                type: "post",
                dataType: "json",
                success: function (msg) {
                    $("#st2").empty();
                    for (m in msg) {
                        var str = "<option value=‘" + msg[m].code + "‘>" + msg[m].name + "</option>";
                        $("#st2").append(str);
                    }
                },
                error: function () {
                    alert("aa");
                },
                beforeSend: function () {
                    $("#st2").append("<option value=‘null‘>加载中...</option>");
                },
                complete: function () {
                    insert("3");
                }
            });
        }
        if (a == "3") {
            $.ajax({
                url: "Handler.ashx",
                data: { "ccode": $("#st2").val() },
                type: "post",
                dataType: "json",
                success: function (msg) {
                    $("#st3").empty();
                    for (m in msg) {
                        var str = "<option value=‘" + msg[m].code + "‘>" + msg[m].name + "</option>";
                        $("#st3").append(str);
                    }
                },
                error: function () {
                    alert("aaa");
                },
                beforeSend: function () {
                    $("#st3").append("<option value=‘null‘>加载中...</option>");
                },

            });
        }
    }
    $("#st1").change(function () {
        insert(2);
    });
    $("#st2").change(function () {
        insert(3);
    });

 

【2017-6-7】AJAX异步刷新 省市区 三级联动

标签:send   option   blog   function   pen   color   ges   联动   数据   

原文地址:http://www.cnblogs.com/hanqi0216/p/6963705.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!