国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ajax.net +jquery 無刷新三級聯(lián)動的實例代碼

2020-01-17 23:55:34
字體:
來源:轉載
供稿:網(wǎng)友

復制代碼 代碼如下:

    <!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要用一個方法失掉數(shù)據(jù)并添加到model中
          //建list實例
            System.Collections.Generic.List<Model.province> list = new System.Collections.Generic.List<Model.province>();
            //已經(jīng)有了的失掉數(shù)據(jù)的方法就不用自己寫了通過調用Getlist的方法操縱數(shù)據(jù)庫拿到數(shù)據(jù)
            DataTable dt = GetList("").Tables[0];
            //拿到數(shù)據(jù)后就需要將數(shù)據(jù)添加到model實例中了

            foreach (DataRow row in dt.Rows)
            {
                //每一行都是個實例所以要將model的放在循環(huán)里面
                Model.province mp = new Model.province();
                mp.provinceID = row["provinceID"].ToString();
                mp.provincename = row["provincename"].ToString();
                list.Add(mp);//沒添加完一個實例都要放到list中
            }
            return list;


        }

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 阳原县| 通辽市| 舟曲县| 沅陵县| 景洪市| 鄂温| 石家庄市| 塘沽区| 邵阳县| 永丰县| 叶城县| 固安县| 莎车县| 贵港市| 竹山县| 石棉县| 伊宁县| 石柱| 稷山县| 上犹县| 龙井市| 沙田区| 临洮县| 绥滨县| 简阳市| 昌黎县| 靖西县| 通江县| 八宿县| 沾化县| 伽师县| 临城县| 正蓝旗| 盐山县| 江孜县| 宁明县| 佳木斯市| 侯马市| 东乌珠穆沁旗| 本溪市| 济源市|