首页 > 代码库 > JS制作的简单的三级及联
JS制作的简单的三级及联
前台:
<form id="form1" runat="server"> <div> 省 <select id="Province"> <option>请选择</option> </select> 市 <select id="City"> <option>请选择</option> </select> 地区 <select id="Space"> <option>请选择</option> </select> <script type="text/javascript"> $(function () { $.ajax({ url: "TestClass/Cascade.ashx?id=1", async: false, success: function (data) { $("#Province").append(data); $("#Province").change(function () { $.ajax({ url: "TestClass/Cascade.ashx?id=2", data: { pid: $("#Province").attr("value") }, success: function (data) { $("#City").html(""); $("#City").append(data); $("#City").change( function () { $.ajax({ url: "TestClass/Cascade.ashx?id=3", data: { cid: $("#City").attr("value") }, success: function (data) { $("#Space").html(""); $("#Space").append(data); } }) }) } }) }) } }) }) </script> </div> </form>
后台:
public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; if (context.Request.QueryString["id"] == "1") { string sql = "SELECT ID,[enname] FROM [ProvinceInfo] WHERE ParentId=0"; StringBuilder sb = new StringBuilder(); SqlDataReader reader = PubSqlHelperFunc.ExecuteSqlDataReader(CommandType.Text, sql, null); while (reader.Read()) { sb.Append("<option value=http://www.mamicode.com/"); sb.Append(reader["Id"]); sb.Append(">"); sb.Append(reader["enname"]); sb.Append("</option>"); } reader.Close(); context.Response.Write(sb.ToString()); } else if (context.Request.QueryString["id"] == "2") { string sql = "SELECT ID,[enname] FROM [ProvinceInfo] WHERE ParentId=" + context.Request.Params["pid"] + ""; StringBuilder sb = new StringBuilder(); sb.Append("<option>请选择</option>"); SqlDataReader reader = PubSqlHelperFunc.ExecuteSqlDataReader(CommandType.Text, sql, null); while (reader.Read()) { sb.Append("<option value=http://www.mamicode.com/"); sb.Append(reader["Id"]); sb.Append(">"); sb.Append(reader["enname"]); sb.Append("</option>"); } reader.Close(); context.Response.Write(sb.ToString()); } else if (context.Request.QueryString["id"] == "3") { string sql = "SELECT ID,[enname] FROM [ProvinceInfo] WHERE ParentId=" + context.Request.Params["cid"] + ""; StringBuilder sb = new StringBuilder(); sb.Append("<option>请选择</option>"); SqlDataReader reader = PubSqlHelperFunc.ExecuteSqlDataReader(CommandType.Text, sql, null); while (reader.Read()) { sb.Append("<option value=http://www.mamicode.com/"); sb.Append(reader["Id"]); sb.Append(">"); sb.Append(reader["enname"]); sb.Append("</option>"); } reader.Close(); context.Response.Write(sb.ToString()); } }
JS制作的简单的三级及联
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。