Ajax版省市三联动(源码)

前端之家收集整理的这篇文章主要介绍了Ajax版省市三联动(源码)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<script src="jquery-1.9.1.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$.ajax({
type: "post",
contentType: "application/json",
url: "WebService1.asmx/GetProvince",
data: "{}",
success: function (result) {
var strprovince;
for (var i = 0; i < result.d.length; i++) {
strprovince += "<option value='" + result.d[i].provinceID + "'>";
strprovince += result.d[i].provincename;
strprovince += "</option>";
}
$("#province").append(strprovince);
}
})
$("#province").change(function () {
$("#city option:gt(0)").remove();
$("#area option:gt(0)").remove();
$.ajax({
type: "post",
url: "WebService1.asmx/GetCity",
data: "{provid:'" + $(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>";
}
$("#city").append(strcity);
}
})
})
$("#city").change(function () {
$("#area option:gt(0)").remove();
$.ajax({
type: "post",
url: "WebService1.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>"
}
$("#area").append(strarea);
}
})
})
})
</script>
</head>
<body>
<select id="province">
<option>----请选择省----</option>
</select>
<select id="city">
<option>----请选择市----</option>
</select>
<select id="area">
<option>----请选择县----</option>
</select>
</body>

*********************************WebService1.asmx页面***********************************

[WebMethod]
public List<fenye.Model.province> GetProvince()
{
fenye.BLL.province prov = new fenye.BLL.province();
List<fenye.Model.province> provlist = prov.GetListModel();
return provlist;
}
[WebMethod]
public List<fenye.Model.city> GetCity(string provid)
{
fenye.BLL.city city = new fenye.BLL.city();
List<fenye.Model.city> citylist = city.GetListcity("father='" + provid + "'");
return citylist;
}
[WebMethod]
public List<fenye.Model.area> GetArea(string cityid)
{
fenye.BLL.area area = new fenye.BLL.area();
List<fenye.Model.area> arealist = area.GetListarea("father='" + cityid + "'");
return arealist;
}

*********************************DAL province********************************************

/// <summary>
/// 获得数据列表
/// </summary>
public DataSet GetList(string strWhere)
{
StringBuilder strsql=new StringBuilder();
strsql.Append("select id,provinceID,provincename ");
strsql.Append(" FROM province ");
if(strWhere.Trim()!="")
{
strsql.Append(" where "+strWhere);
}
return DbHelpersql.Query(strsql.ToString());
}

//获取数据列表
public List<Model.province> GetListModel()
{
List<Model.province> listprov = new List<Model.province>();
DataTable dt = GetList("").Tables[0];
foreach (DataRow row in dt.Rows)
{
Model.province prov = new Model.province();
prov.id = Convert.ToInt32(row["id"]);
prov.provinceID = row["provinceID"].ToString();
prov.provincename = row["provincename"].ToString();
listprov.Add(prov);
}
return listprov;
}

***********************************DAL city*************************************************

/// <summary>
/// 获得数据列表
/// </summary>
public DataSet GetList(string strWhere)
{
StringBuilder strsql=new StringBuilder();
strsql.Append("select id,cityID,cityname,father ");
strsql.Append(" FROM city ");
if(strWhere.Trim()!="")
{
strsql.Append(" where "+strWhere);
}
return DbHelpersql.Query(strsql.ToString());
}

public List<Model.city> GetListcity(string strsql)
{
List<Model.city> listcity = new List<Model.city>();
DataTable dt = GetList(strsql).Tables[0];
foreach (DataRow row in dt.Rows)
{
Model.city city = new Model.city();
city.cityID = row["cityID"].ToString();
city.cityname = row["cityname"].ToString();
listcity.Add(city);
}
return listcity;
}

*************************DAL area*************************************

/// <summary>
/// 获得数据列表
/// </summary>
public DataSet GetList(string strWhere)
{
StringBuilder strsql=new StringBuilder();
strsql.Append("select id,areaID,areaname,father ");
strsql.Append(" FROM area ");
if(strWhere.Trim()!="")
{
strsql.Append(" where "+strWhere);
}
return DbHelpersql.Query(strsql.ToString());
}

public List<Model.area> GetListarea(string sqlStr)
{
List<Model.area> listarea = new List<Model.area>();
DataTable dt = GetList(sqlStr).Tables[0];
foreach (DataRow row in dt.Rows)
{
Model.area area = new Model.area();
area.areaID = row["areaID"].ToString();
area.areaname = row["areaname"].ToString();
listarea.Add(area);
}
return listarea;
}

********************************BLL provice***************************************

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

*********************************BLL city******************************************

public List<Model.city> GetListcity(string sqlStr)
{
return dal.GetListcity(sqlStr);
}

*********************************BLL area********************************************

public List<Model.area> GetListarea(string sqlStr) { return dal.GetListarea(sqlStr); }

原文链接:https://www.f2er.com/ajax/166328.html

猜你在找的Ajax相关文章