ajax.net +jquery 無重新整理三級聯動的執行個體代碼_實用技巧

來源:互聯網
上載者:User

複製代碼 代碼如下:

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


        }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.