欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

ajax.net +jquery 无刷新三级联动的实例代码

程序员文章站 2024-03-01 12:25:10
复制代码 代码如下:   

复制代码 代码如下:

    <!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>
    <title></title>
    <script src="jquery1.7.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $.ajax({
                type: "post",
                contenttype: "application/json",
                url: "webservice1.asmx/loadprovince",
                data: "{}",
                success: function (result) {
                    var ping;
                    for (var i = 0; i < result.d.length; i++) {
                        ping += "<option value=" + result.d[i].provinceid + ">";
                        ping += result.d[i].provincename;
                        ping += "</option>";

                    }
                    $('#select1').append(ping);

   
                }

   
            })
            $('#select1').change(function () {
                //第二次选时要记得清空市和县中的内容
                $('#select2 option:gt(0)').remove();
                $('#select3 option:gt(0)').remove();
                //在省的改变事件里绑定下一个下来列表(要失掉省的id)

                $.ajax({
                    type: "post",
                    contenttype: "application/json",
                    url: "webservice1.asmx/loadcity",
                    data: "{fatherid:'" + $('#select1 option:selected').val() + "'}",
                    success: function (result) {
                        var str2;
                        for (var i = 0; i < result.d.length; i++) {
                            str2 += "<option value=" + result.d[i].cityid + ">";
                            str2 += result.d[i].cityname;
                            str2 += "</option>";
                        }

                        $('#select2').append(str2);
                    }
                })
            })
            $('#select2').change(function () {
                $('#select3 option:gt(0)').remove();

                $.ajax({
                    type: "post",
                    contenttype: "application/json",
                    url: "webservice1.asmx/loadarea",
                    data: "{fatherid:'" + $('#select2 option:selected').val() + "'}",
                    success: function (result) {
                        var str3;
                        for (var i = 0; i < result.d.length; i++) {
                            str3 += "<option value=" + result.d.areaid + ">";
                            str3 += result.d[i].areaname;
                            str3 += result.d[i].father;

                        }
                        $('#select3').append(str3);
                    }
                })
            })
        })

    </script>
</head>
<body>
   省:
    <select id="select1">
        <option>--请选择--</option>
    </select>
    市:
    <select id="select2">
        <option>--请选择--</option>
    </select>
    县:
    <select id="select3">
        <option>--请选择--</option>
    </select>
</body>
</html>

webservice:

复制代码 代码如下:

 [webmethod]//加载省
        public list<model.province> loadprovince()//为什么要用list因为这里出从前的值不可能一个实例是多个model实例,一个实例就是一条记载这样做防止字段错误
        {
            bll.province bp = new bll.province();
            list<model.province> list=bp.getlistmodel();
            return list;
        }
        [webmethod]//加载市
        public list<model.city> loadcity(string fatherid)
        {
            bll.city bc = new bll.city();
            list<model.city> list = bc.getlistmodel(fatherid);
            return list;
        }
        [webmethod]//加载县
        public list<model.area> loadarea(string fatherid)
        {
            bll.area ba = new bll.area();
            list<model.area> list = ba.getlistmodel(fatherid);
            return list;

        }
    }
}

dal--area

复制代码 代码如下:

public system.collections.generic.list<model.area> getlistmodel(string fatherid)
        {
            system.collections.generic.list<model.area> list = new system.collections.generic.list<model.area>();
            datatable dt = getlist("father=" + fatherid + "").tables[0];
            foreach (datarow row in dt.rows)
            {
                model.area ma = new model.area();
                ma.areaid = row["areaid"].tostring();
                ma.areaname = row["areaname"].tostring();
                ma.father = row["father"].tostring();
                list.add(ma);
            }
            return list;
        }


dal--city

复制代码 代码如下:

 public system.collections.generic.list<model.area> getlistmodel(string fatherid)
        {
            system.collections.generic.list<model.area> list = new system.collections.generic.list<model.area>();
            datatable dt = getlist("father=" + fatherid + "").tables[0];
            foreach (datarow row in dt.rows)
            {
                model.area ma = new model.area();
                ma.areaid = row["areaid"].tostring();
                ma.areaname = row["areaname"].tostring();
                ma.father = row["father"].tostring();
                list.add(ma);
            }
            return list;
        }
    }


dal-provience

复制代码 代码如下:

public system.collections.generic.list<model.province> getlistmodel()
        {
            //将要查的内容以实例的方式返回
            //这里要做的就是要建立list并将list用model实例填满,而model要用一个方法失掉数据并添加到model中
          //建list实例
            system.collections.generic.list<model.province> list = new system.collections.generic.list<model.province>();
            //已经有了的失掉数据的方法就不用自己写了通过调用getlist的方法操纵数据库拿到数据
            datatable dt = getlist("").tables[0];
            //拿到数据后就需要将数据添加到model实例中了

            foreach (datarow row in dt.rows)
            {
                //每一行都是个实例所以要将model的放在循环里面
                model.province mp = new model.province();
                mp.provinceid = row["provinceid"].tostring();
                mp.provincename = row["provincename"].tostring();
                list.add(mp);//没添加完一个实例都要放到list中
            }
            return list;


        }