首页 > 代码库 > ajax 三级联动
ajax 三级联动
<select class="st" id="st1"> <option value=http://www.mamicode.com/"null">加载中...</option> </select> <select class="st" id="st2"> <option value=http://www.mamicode.com/"null">加载中...</option> </select> <select class="st" id="st3"> <option value=http://www.mamicode.com/"null">加载中...</option> </select>
<script type="text/javascript"> statesload(‘1‘); function statesload(a) { if (a == "1") { $.ajax({ url: "ccc.ashx", data: { "pcode": "0001" }, type: "post", dataType: "json", success: function (msg) { $("#st1").html(‘‘); for (i in msg) { var str = "<option value=http://www.mamicode.com/‘" + msg[i].code + "‘>" + msg[i].name + "</option>"; $("#st1").append(str); } statesload(‘2‘); }, beforeSend: function () { $("#st1").html(‘‘); $("#st1").append("<option value=http://www.mamicode.com/‘null‘>加载中..."); } }); } if (a == "2") { $.ajax({ url: "ccc.ashx", data: { "pcode": $("#st1").val() }, type: "post", dataType: "json", success: function (msg) { $("#st2").html(‘‘); for (i in msg) { var str = "<option value=http://www.mamicode.com/‘" + msg[i].code + "‘>" + msg[i].name + "</option>"; $("#st2").append(str); } statesload(‘3‘); }, beforeSend: function () { $("#st2").html(‘‘); $("#st2").append("<option value=http://www.mamicode.com/‘null‘>加载中..."); } }); } if (a == "3") { $.ajax({ url: "ccc.ashx", data: { "pcode": $("#st2").val() }, type: "post", dataType: "json", success: function (msg) { $("#st3").html(‘‘); for (i in msg) { var str = "<option value=http://www.mamicode.com/‘" + msg[i].code + "‘>" + msg[i].name + "</option>"; $("#st3").append(str); } }, beforeSend: function () { $("#st3").html(‘‘); $("#st3").append("<option value=http://www.mamicode.com/‘null‘>加载中..."); } }); } } $("#st1").change(function () { statesload(‘2‘); }); $("#st2").change(function () { statesload(‘3‘); }); </script>
后台
public void ProcessRequest(HttpContext context) { System.Threading.Thread.Sleep(2000); StringBuilder str = new StringBuilder(); str.Append("["); string s = context.Request["pcode"]; using (mydbDataContext con = new mydbDataContext()) { 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(); }
ajax 三级联动
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。