【2017-6-7】AJAX非同步重新整理 省市區 三級聯動

來源:互聯網
上載者:User

標籤: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非同步重新整理 省市區 三級聯動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.