1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="JqueryDemoTest.WebForm2" %> <! 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 runat="server"> < title ></ title > < script src="js/jquery-1.4.1.min.js" type="text/javascript"></ script > < script src="js/jquery-1.4.1-vsdoc.js" type="text/javascript"></ script > < script type="text/javascript"> $(function () { $('#btn').click(function () { $.post('Handler.ashx', { val: $("#<%=ddlMain.ClientID %>").val() }, function (data) { var json = eval(data); var html = ''; for (var i in json) { html += '< option value="' + json[i].Value + '">' + json[i].Name + '</ option >'; } $("#<%=ddlSub.ClientID %>")[0].options.length = 0; $(html).appendTo($("#<%=ddlSub.ClientID %>")); }); }); }); </ script > </ head > < body > < form id="form1" runat="server"> < div > < asp:DropDownList ID="ddlMain" runat="server"> < asp:ListItem Text="一级城市" Value="1"></ asp:ListItem > < asp:ListItem Text="旅游城市" Value="2"></ asp:ListItem > </ asp:DropDownList > < asp:DropDownList ID="ddlSub" runat="server"> </ asp:DropDownList > < input type="button" id='btn' value='click me' /> </ div > </ form > </ body > </ html > |
code ashx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace JqueryDemoTest { /// <summary> /// Summary description for Handler /// </summary> public class Handler : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain" ; string json = string .Empty; if (context.Request.Form[ "val" ] == "1" ) { json = "[{\"Name\":\"北京\",\"Value\":1},{\"Name\":\"深圳\",\"Value\":2},{\"Name\":\"上海\",\"Value\":3}]" ; } else if (context.Request.Form[ "val" ] == "2" ) { json = "[{\"Name\":\"杭州\",\"Value\":4},{\"Name\":\"苏州\",\"Value\":5},{\"Name\":\"桂林\",\"Value\":6}]" ; } context.Response.Write(json); context.Response.End(); } public bool IsReusable { get { return false ; } } } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步