温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ajax.net +jquery如何实现无刷新三级联动

发布时间:2021-10-14 09:13:16 来源:亿速云 阅读:103 作者:小新 栏目:开发技术

这篇文章主要介绍了ajax.net +jquery如何实现无刷新三级联动,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

 代码如下:


    <!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;


        }

感谢你能够认真阅读完这篇文章,希望小编分享的“ajax.net +jquery如何实现无刷新三级联动”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI