Jquery ajax 并发的下拉框

本文介绍了一种使用Ajax和JavaScript实现省市联动下拉框的方法。通过选择省份时触发Ajax请求,动态更新城市下拉框的内容。具体实现包括前端HTML、ASP.NET元素以及后端处理逻辑。

前台


<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" onchange="change(this)">
        </asp:DropDownList>
        <select id="select_city">
        </select>
    </div>
    </form>

    <script type="text/javascript">


        function change(obj) {
            var province_ID = $(obj).children("option:selected").attr("Value");
            $.ajax({
                url: '/Services/getCity.ashx',
                data: { "province_ID": $(obj).children("option:selected").attr("Value") },
                type: 'POST',
                dataType: 'text',
                success: function(data) {
                    var json = $.parseJSON(data);
                    $select_city = $("#select_city");
                    $select_city.children().remove();
                    $.each(json, function(i, item) {
                        $select_city.append("<option value='" + item.City_ID + "'>" + item.City_Name + "</option>");

                    });
                },
                error: function() {
                    alert('error !');
                }

            });
        }


后台

 public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string province_ID = context.Request.Params["province_ID"];
           DataTable dt = SingleTon<SqlDataAccess>.instance.getTable("select t.City_ID,t.City_Name from City t where t.Province_ID='"+province_ID+"'");
           string json = JsonHelper.getJson(dt, "City_ID", "City_Name");
           
            context.Response.Write(json);
        }
   public static string getJson(DataTable dt, params string[] pars)
        {

            string json = string.Empty;
            if (dt == null)
                return json;
            json += "[";
            foreach (DataRow dr in dt.Rows)
            {
                string temp = "{";
                foreach (string par in pars)
                {
                    temp += string.Format("\"{0}\":\"{1}\",", par, dr[par]);
                }
                temp = temp.Remove(temp.Length - 1, 1);
                temp += "},";
                json += temp;
            }

            json = json.Remove(json.Length - 1, 1);
            json += "]";

            return json;
        }




评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值