zoukankan      html  css  js  c++  java
  • JQuery+ajax 三级联动

    html中代码:

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
        <script src="js/jquery-1.7.2.min.js"></script>
    </head>
    <body>
        <select id="sel1">
            <option>加载中...</option>
        </select>
        <select id="sel2">
            <option>加载中...</option>
        </select>
        <select id="sel3">
            <option>加载中...</option>
        </select>
    
        <script type="text/javascript">
            a($("#sel1"), "0001", "1");
    
            $("#sel1").change(function () {
                a($("#sel2"), $("#sel1").val(), "2");
            });
    
            $("#sel2").change(function () {
                a($("#sel3"), $("#sel2").val(), "3");
            });
    
            function a(sel, code, count) {
                $.ajax({
                    url: "ashxs/china.ashx",
                    data: { "code": code },
                    type: "post",
                    dataType: "json",
                    success: function (data) {
                        sel.empty();
                        for (i in data) {
                            sel.get(0).add(new Option(data[i].name, data[i].code));
                        }
                        if (count == "1") {
                            a($("#sel2"), $("#sel1").val(), "2");
                        }
                        if (count == "2") {
                            a($("#sel3"), $("#sel2").val(), "3");
                        }
                    }
                });//ajax
            }
    
        </script>
    </body>
    </html>

    一般处理程序中的代码:

    <%@ WebHandler Language="C#" Class="china" %>
    
    using System;
    using System.Web;
    using System.Linq;
    using System.Data.Linq;
    using System.Collections;
    using System.Collections.Generic;
    
    public class china : IHttpHandler
    {
        DataClassesDataContext con = new DataClassesDataContext();
        public void ProcessRequest(HttpContext context)
        {
            string end = "[";
            int count = 0;
            string code = context.Request["code"];
            List<ChinaStates> list = con.ChinaStates.Where(r => r.ParentAreaCode == code).ToList();
            if (list.Count > 0)
            {
                foreach (ChinaStates c in list)
                {
                    if (count <= 0)
                    {
                        end += "{"name":"" + c.AreaName + "","code":"" + c.AreaCode + ""}";
                    }
                    else
                    {
                        end += ",{"name":"" + c.AreaName + "","code":"" + c.AreaCode + ""}";
                    }
                    count++;
                }
            }
            end += "]";
            context.Response.Write(end);
        }
    
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    
    }
  • 相关阅读:
    vsphere client cd/dvd 驱动器1 正在连接
    使用SecureCRT上传和下载文件
    java android 将 List中元素互换位置
    java中Math常用方法
    关于View转化成bitmap保存成图片
    Java中Math类的几个四舍五入方法的区别
    Math.round(),Math.ceil(),Math.floor()的区别
    动态的添加ImageView到LinearLayout中并居中显示
    android之View坐标系(view获取自身坐标的方法和点击事件中坐标的获取)
    Android 代码设置RelativeLayout元素居中
  • 原文地址:https://www.cnblogs.com/123lucy/p/5769713.html
Copyright © 2011-2022 走看看