省市县三级联动无刷新版本(AJAX\html\WebService\三层)

AJAX 专栏收录该内容
10 篇文章 0 订阅

前台html代码

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <style type="text/css">
    select
    {
        width:150px;
        }
    </style>
    <script src="Jquery1.7.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            $.ajax({
                type: "post",
                contentType: "application/json",
                url: "WebService2.asmx/Loadprovince",
                data: "{}",
                success: function (result) {
                    for (var i = 0; i < result.d.length; i++) {
                        var stroption = '<option value=' + result.d[i].provinceID + '>';
                        stroption += result.d[i].provincename;
                        stroption += '</option>';
                        $('#seprovince').append(stroption);
                    }
                }
            })
            $('#seprovince').change(function () {
                $('#secity option:gt(0)').remove();
                $.ajax
                            ({
                                type: "post",
                                contentType: "application/json",
                                url: "WebService2.asmx/Getcity",
                                data: "{provinceid:'" + $(this).val() + "'}",
                                success: function (result) {
                                    var strcity = '';
                                    for (var i = 0; i < result.d.length; i++) {
                                        strcity += '<option value=' + result.d[i].cityID + '>';
                                        strcity += result.d[i].cityname;
                                        strcity += '</option>';
                                    }
                                    $('#secity').append(strcity);
                                }
                            })
            })

            $('#secity').change(function () {
                $('#searea option:gt(0)').remove();
                $.ajax({
                    type: "post",
                    contentType: "application/json",
                    url: "WebService2.asmx/Getarea",
                    data: "{cityid:'" + $(this).val() + "'}",
                    success: function (result) {
                        var strarea = '';
                        for (var i = 0; i < result.d.length; i++) {
                            strarea += '<option value=' + result.d[i].areaID + '>';
                            strarea += result.d[i].areaname;
                            strarea += '</option>';
                        }
                        $('#searea').append(strarea);
                    }
                })

            })
        })
    </script>
</head>
<body>
<div>
<table>
<tr>
<td>省</td>
<td>
<select id="seprovince">
<option>--请选择--</option>
</select>
</td>
</tr>
<tr>
<td>市</td>
<td>
<select id="secity">
<option>--请选择--</option>
</select>
</td>
</tr>
<tr>
<td>县</td>
<td>
<select id="searea">
<option>--请选择--</option>
</select>
</td>
</tr>
</table>
</div>
</body>
</html>

WebService页面代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace 三联动
{
    /// <summary>
    /// WebService2 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
     [System.Web.Script.Services.ScriptService]
    public class WebService2 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
        [WebMethod]
        public List<WebAJAX.Model.province> Loadprovince()
        {
            WebAJAX.BLL.province bprovince = new WebAJAX.BLL.province();
            List<WebAJAX.Model.province> list = bprovince.GetListModel();
            return list;
        }
        [WebMethod]
        public List<WebAJAX.Model.city> Getcity(string provinceid)
        {
            WebAJAX.BLL.city bcity = new WebAJAX.BLL.city();
            List<WebAJAX.Model.city> list = bcity.GetListModel("father='"+provinceid+"'");
            return list;
        }
        [WebMethod]
        public List<WebAJAX.Model.area> Getarea(string cityid)
        {
            WebAJAX.BLL.area bllarea = new WebAJAX.BLL.area();
           List<WebAJAX.Model.area> list= bllarea.GetListModel("father='" + cityid + "'");
           return list;
        }
    }
}

三层BLL代码

public List<Model.province> GetListModel()
        {
            return dal.GetListModel();
        }

public List<Model.city> GetListModel(string strsql)
        {
            return dal.GetListModel(strsql);
        }

public List<Model.area> GetListModel(string strsql)
        {
            return dal.GetListModel(strsql);
        }

三层DAL代码

public System.Collections.Generic.List<Model.province> GetListModel()
        {
           System.Collections.Generic.List<Model.province> list = new System.Collections.Generic.List<Model.province>();
           DataTable dt= GetList("").Tables[0];
           foreach (DataRow row in dt.Rows)
           {
               Model.province mpro = new Model.province();
               mpro.id = Convert.ToInt32(row["id"]);
               mpro.provinceID = row["provinceID"].ToString();
               mpro.provincename = row["provincename"].ToString();
               list.Add(mpro);
           }
           return list;
        }

public System.Collections.Generic.List<Model.city> GetListModel(string strsql)
        {
            System.Collections.Generic.List<Model.city> list = new System.Collections.Generic.List<Model.city>();
            DataTable dt = GetList(strsql).Tables[0];
            foreach (DataRow row in dt.Rows)
            {
                Model.city mcity = new Model.city();
                mcity.id = Convert.ToInt32(row["id"]);
                mcity.cityID = row["cityID"].ToString();
                mcity.cityname = row["cityname"].ToString();
                list.Add(mcity);
            }
            return list;
        }

public System.Collections.Generic.List<Model.area> GetListModer(string strsql)
        {
            System.Collections.Generic.List<Model.area> list = new System.Collections.Generic.List<Model.area>();
            DataTable dt= GetList(strsql).Tables[0];
            foreach (DataRow row in dt.Rows)
            {
                Model.area marea = new Model.area();
                marea.id = Convert.ToInt32(row["id"]);
                marea.areaID = row["areaID"].ToString();
                marea.areaname = row["areaname"].ToString();
                list.Add(marea);
            }
            return list;
        }

  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值