jquery 實(shí)現(xiàn)二級(jí)/三級(jí)/多級(jí)聯(lián)動(dòng)菜單的思路及代碼
更新時(shí)間:2013年04月08日 10:33:15 作者:
jQuery的AJAX功能和asp.net來實(shí)現(xiàn)省市區(qū)的三級(jí)聯(lián)動(dòng)效果,其他二級(jí)、三級(jí)或多級(jí)聯(lián)動(dòng)也可以按照此方法完成,由此需求的朋友可以參考下哈,希望可以幫助到你
本文介紹使用jQuery的AJAX功能和asp.net來實(shí)現(xiàn)省市區(qū)的三級(jí)聯(lián)動(dòng)效果,其他二級(jí)、三級(jí)或多級(jí)聯(lián)動(dòng)也可以按照此方法完成。
文章中涉及到的數(shù)據(jù)表為City,為方便管理。
設(shè)計(jì)此表如下
ID:自增長字段
City_Name:城市名稱
City_Code:城市代碼
我們根據(jù)城市代碼來查詢省、市、區(qū)。城市代碼結(jié)構(gòu)大致如下:
內(nèi)蒙古:150000,呼和浩特:150100,新城區(qū):150101。
其他地區(qū)代碼與此一樣,省級(jí)為省級(jí)編號(hào)+0000,市級(jí)為省級(jí)編號(hào)+市級(jí)編號(hào)+00,地區(qū)為省級(jí)編號(hào)+市級(jí)編號(hào)+地區(qū)編號(hào)。
City.ASPX代碼為:
<!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 runat="server">
<title>jQuery + ASP.NET實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)</title>
<script src="/Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<style type="text/css">
#dpCity{ display:none; position:relative;}
#dpArea{ display:none;position:relative;}
</style>
</head>
<body>
<div>
省:<asp:DropDownList ID="dpProvince" runat="server"></asp:DropDownList>
市:<asp:DropDownList ID="dpCity" runat="server"></asp:DropDownList>
區(qū):<asp:DropDownList ID="dpArea" runat="server"></asp:DropDownList>
</div>
</body>
</html>
JS代碼:
jQuery(document).ready(function () {
var dp1 = jQuery("#dpProvince");
var dp2 = jQuery("#dpCity");
var dp3 = jQuery("#dpArea");
//填充省的數(shù)據(jù)
loadAreas("", "dpProvince");
//給省綁定事件,觸發(fā)事件后填充市的數(shù)據(jù)
jQuery(dp1).bind("change keyup", function () {
var provinceID = dp1.attr("value");
loadAreas(provinceID, "dpCity");
dp2.fadeIn("slow");
});
//給市綁定事件,觸發(fā)事件后填充區(qū)的數(shù)據(jù)
jQuery(dp2).bind("change keyup", function () {
var cityID = dp2.attr("value");
loadAreas(cityID, "dpArea");
dp3.fadeIn("slow");
});
});
function loadAreas(val, item) {
jQuery.ajax({
type: "post",
url: "CityLoader.asmx/GetCityList",
data: {
code: val,
a: Math.random()
},
error: function () {
return false;
},
success: function (data) {
var i;
var json = eval(data);
jQuery("#" + item).append("<option value='' selected='selected'>請(qǐng)選擇</option>");
for (i = 0; i < json.length; i++) {
jQuery("#" + item).append(jQuery("<option></option>").val(json[i].c_code).html(json[i].c_name));
};
}
});
}
后臺(tái)代碼:
//實(shí)例類
public class CityModel
{
int _id;
string _cityname;
string _citycode;
public int ID
{
set { _id = value; }
get { return _id; }
}
public string CityName
{
set { _cityname = value; }
get { return _cityname; }
}
public string CityCode
{
set { _citycode = value; }
get { return _citycode; }
}
}
//DAL層,返回DataTable,使用通用SqlHelper
public DataTable CityList(string pcode)
{
string SQL = "SELECT * FROM city WHERE 1=1";
if (!string.IsNullOrEmpty(pcode))
{
if (pcode.Substring(2, 2) != "00")
{
SQL = SQL + " AND RIGHT(citycode,2)<>'00' AND LEFT(citycode,4)=LEFT(@pcode,4)";
}
else
{
SQL = SQL + " AND RIGHT(citycode,2)='00' AND LEFT(RIGHT(citycode,4),2)<>'00' AND LEFT(citycode,2)=LEFT(@pcode,2)";
}
}
else
{
SQL = SQL + " AND LEFT(citycode,2)<>'00' AND RIGHT(citycode,4)='0000'";
}
SQL = SQL + " ORDER BY sorts ASC";
SqlParameter[] Param ={
new SqlParameter("@pcode",pcode)
};
using (SqlConnection conn = new SqlConnection(DBUtility.SqlHelper.ConnectionStringLocalTransaction))
{
DataSet ds = DBUtility.SqlHelper.ExecuteDataSet(conn, CommandType.Text, SQL, Param);
return ds.Tables[0];
}
}
//BLL層,返回City的泛型列表
public List<CityModel> CityList(string code)
{
List<CityModel> list = new List<CityModel>();
DAL. CityDAL cd = new DAL.CityDAL();
DataTable dt = cd.CityList(code);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
CityModel cm = new CityModel();
cm.ID = int.Parse(dt.Rows[i]["id"].ToString());
cm.CityName = dt.Rows[i]["cityname"].ToString();
cm.CityCode = dt.Rows[i]["citycode"].ToString();
list.Add(cm);
}
}
return list;
}
CityLoader.asmx:
/// <summary>
/// CityLoader 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請(qǐng)取消對(duì)下行的注釋。
// [System.Web.Script.Services.ScriptService]
public class CityLoader : System.Web.Services.WebService
{
[WebMethod]
public void GetCityList(string code)
{
CityBLL cb = new CityBLL();
StringBuilder sb = new StringBuilder();
List<CityModel> cm = cb.CityList(code);
sb.Append("[");
if (cm.Count > 0)
{
for (int i = 0; i < cm.Count; i++)
{
CityModel model = cm[i];
sb.Append("{");
sb.AppendFormat(@"""c_name"":""{0}"",", model.CityName);
sb.AppendFormat(@"""c_code"":""{0}""", model.CityCode);
sb.Append("}");
if (i < cm.Count - 1)
{
sb.Append(",");
}
}
}
sb.Append("]");
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
System.Web.HttpContext.Current.Response.Write(sb.ToString());
}
}
文章中涉及到的數(shù)據(jù)表為City,為方便管理。
設(shè)計(jì)此表如下
ID:自增長字段
City_Name:城市名稱
City_Code:城市代碼
我們根據(jù)城市代碼來查詢省、市、區(qū)。城市代碼結(jié)構(gòu)大致如下:
內(nèi)蒙古:150000,呼和浩特:150100,新城區(qū):150101。
其他地區(qū)代碼與此一樣,省級(jí)為省級(jí)編號(hào)+0000,市級(jí)為省級(jí)編號(hào)+市級(jí)編號(hào)+00,地區(qū)為省級(jí)編號(hào)+市級(jí)編號(hào)+地區(qū)編號(hào)。
City.ASPX代碼為:
復(fù)制代碼 代碼如下:
<!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 runat="server">
<title>jQuery + ASP.NET實(shí)現(xiàn)三級(jí)聯(lián)動(dòng)</title>
<script src="/Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<style type="text/css">
#dpCity{ display:none; position:relative;}
#dpArea{ display:none;position:relative;}
</style>
</head>
<body>
<div>
省:<asp:DropDownList ID="dpProvince" runat="server"></asp:DropDownList>
市:<asp:DropDownList ID="dpCity" runat="server"></asp:DropDownList>
區(qū):<asp:DropDownList ID="dpArea" runat="server"></asp:DropDownList>
</div>
</body>
</html>
JS代碼:
復(fù)制代碼 代碼如下:
jQuery(document).ready(function () {
var dp1 = jQuery("#dpProvince");
var dp2 = jQuery("#dpCity");
var dp3 = jQuery("#dpArea");
//填充省的數(shù)據(jù)
loadAreas("", "dpProvince");
//給省綁定事件,觸發(fā)事件后填充市的數(shù)據(jù)
jQuery(dp1).bind("change keyup", function () {
var provinceID = dp1.attr("value");
loadAreas(provinceID, "dpCity");
dp2.fadeIn("slow");
});
//給市綁定事件,觸發(fā)事件后填充區(qū)的數(shù)據(jù)
jQuery(dp2).bind("change keyup", function () {
var cityID = dp2.attr("value");
loadAreas(cityID, "dpArea");
dp3.fadeIn("slow");
});
});
function loadAreas(val, item) {
jQuery.ajax({
type: "post",
url: "CityLoader.asmx/GetCityList",
data: {
code: val,
a: Math.random()
},
error: function () {
return false;
},
success: function (data) {
var i;
var json = eval(data);
jQuery("#" + item).append("<option value='' selected='selected'>請(qǐng)選擇</option>");
for (i = 0; i < json.length; i++) {
jQuery("#" + item).append(jQuery("<option></option>").val(json[i].c_code).html(json[i].c_name));
};
}
});
}
后臺(tái)代碼:
復(fù)制代碼 代碼如下:
//實(shí)例類
public class CityModel
{
int _id;
string _cityname;
string _citycode;
public int ID
{
set { _id = value; }
get { return _id; }
}
public string CityName
{
set { _cityname = value; }
get { return _cityname; }
}
public string CityCode
{
set { _citycode = value; }
get { return _citycode; }
}
}
//DAL層,返回DataTable,使用通用SqlHelper
public DataTable CityList(string pcode)
{
string SQL = "SELECT * FROM city WHERE 1=1";
if (!string.IsNullOrEmpty(pcode))
{
if (pcode.Substring(2, 2) != "00")
{
SQL = SQL + " AND RIGHT(citycode,2)<>'00' AND LEFT(citycode,4)=LEFT(@pcode,4)";
}
else
{
SQL = SQL + " AND RIGHT(citycode,2)='00' AND LEFT(RIGHT(citycode,4),2)<>'00' AND LEFT(citycode,2)=LEFT(@pcode,2)";
}
}
else
{
SQL = SQL + " AND LEFT(citycode,2)<>'00' AND RIGHT(citycode,4)='0000'";
}
SQL = SQL + " ORDER BY sorts ASC";
SqlParameter[] Param ={
new SqlParameter("@pcode",pcode)
};
using (SqlConnection conn = new SqlConnection(DBUtility.SqlHelper.ConnectionStringLocalTransaction))
{
DataSet ds = DBUtility.SqlHelper.ExecuteDataSet(conn, CommandType.Text, SQL, Param);
return ds.Tables[0];
}
}
//BLL層,返回City的泛型列表
public List<CityModel> CityList(string code)
{
List<CityModel> list = new List<CityModel>();
DAL. CityDAL cd = new DAL.CityDAL();
DataTable dt = cd.CityList(code);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
CityModel cm = new CityModel();
cm.ID = int.Parse(dt.Rows[i]["id"].ToString());
cm.CityName = dt.Rows[i]["cityname"].ToString();
cm.CityCode = dt.Rows[i]["citycode"].ToString();
list.Add(cm);
}
}
return list;
}
CityLoader.asmx:
復(fù)制代碼 代碼如下:
/// <summary>
/// CityLoader 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允許使用 ASP.NET AJAX 從腳本中調(diào)用此 Web 服務(wù),請(qǐng)取消對(duì)下行的注釋。
// [System.Web.Script.Services.ScriptService]
public class CityLoader : System.Web.Services.WebService
{
[WebMethod]
public void GetCityList(string code)
{
CityBLL cb = new CityBLL();
StringBuilder sb = new StringBuilder();
List<CityModel> cm = cb.CityList(code);
sb.Append("[");
if (cm.Count > 0)
{
for (int i = 0; i < cm.Count; i++)
{
CityModel model = cm[i];
sb.Append("{");
sb.AppendFormat(@"""c_name"":""{0}"",", model.CityName);
sb.AppendFormat(@"""c_code"":""{0}""", model.CityCode);
sb.Append("}");
if (i < cm.Count - 1)
{
sb.Append(",");
}
}
}
sb.Append("]");
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
System.Web.HttpContext.Current.Response.Write(sb.ToString());
}
}
您可能感興趣的文章:
- jquery+json 通用三級(jí)聯(lián)動(dòng)下拉列表
- jQuery結(jié)合PHP+MySQL實(shí)現(xiàn)二級(jí)聯(lián)動(dòng)下拉列表[實(shí)例]
- jQuery實(shí)現(xiàn)多級(jí)聯(lián)動(dòng)下拉列表查詢框
- jQuery 下拉列表 二級(jí)聯(lián)動(dòng)插件分享
- jQuery ajax+PHP實(shí)現(xiàn)的級(jí)聯(lián)下拉列表框功能示例
- 基于jquery的二級(jí)聯(lián)動(dòng)菜單實(shí)現(xiàn)代碼
- 簡單實(shí)用jquery版三級(jí)聯(lián)動(dòng)select示例
- JQuery實(shí)現(xiàn)級(jí)聯(lián)下拉框效果實(shí)例講解
- 省市區(qū)三級(jí)聯(lián)動(dòng)jquery實(shí)現(xiàn)代碼
- 基于jquery的無限級(jí)聯(lián)下拉框js插件
- jQuery實(shí)現(xiàn)動(dòng)態(tài)生成年月日級(jí)聯(lián)下拉列表示例
相關(guān)文章
jQuery:節(jié)點(diǎn)(插入,復(fù)制,替換,刪除)操作
jQuery:節(jié)點(diǎn)(插入,復(fù)制,替換,刪除)操作,需要的朋友可以參考一下2013-03-03基于jQuery實(shí)現(xiàn)左右圖片輪播(原理通用)
這篇文章主要介紹了基于jQuery實(shí)現(xiàn)左右圖片輪播,實(shí)現(xiàn)原理通用可通用,希望能和大家分享一下思路,感興趣的小伙伴們可以參考一下2015-12-12jquery基礎(chǔ)教程之?dāng)?shù)組使用詳解
jQuery的數(shù)組處理、便捷、功能齊全。一步到位的封裝了很多原生JavaScript數(shù)組不能企及的功能。下面是jquery數(shù)組的使用詳解,需要的朋友可以參考下2014-03-03JQuery 簡便實(shí)現(xiàn)頁面元素?cái)?shù)據(jù)驗(yàn)證功能
JQuery 簡便實(shí)現(xiàn)頁面元素?cái)?shù)據(jù)驗(yàn)證功能...2007-03-03jquery實(shí)現(xiàn)獲取具體時(shí)間(年月日)后3個(gè)月+1天的年月日
這篇文章主要為大家詳細(xì)介紹了jquery如何實(shí)現(xiàn)獲取具體時(shí)間(年月日)后3個(gè)月+1天的年月日,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下2023-11-11jquery的ajax提交form表單的兩種方法小結(jié)(推薦)
下面小編就為大家?guī)硪黄猨query的ajax提交form表單的兩種方法小結(jié)(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考2016-05-05