基于jQuery Ajax實(shí)現(xiàn)下拉框無刷新聯(lián)動(dòng)
本文實(shí)例為大家分享了jQuery Ajax實(shí)現(xiàn)下拉框無刷新聯(lián)動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
HTML代碼:
@{ Layout = null; } @using DAL; @using System.Data; @{ AreaDal areaDal = new AreaDal(); string areaId = ViewBag.areaId; DataRow drArea = areaDal.GetArea(areaId); string countyId = drArea == null ? "-1" : drArea["countyId"].ToString(); DataRow drCounty = areaDal.GetCounty(countyId); string cityId = drCounty == null ? "-1" : drCounty["cityId"].ToString(); DataRow drCity = areaDal.GetCity(cityId); string provinceId = drCity == null ? "-1" : drCity["provinceId"].ToString(); } <!DOCTYPE html> <html> <head> <title>商圈選擇</title> <script type="text/javascript"> //選中 function select(selId, id, callback) { $("#" + selId).val(id); if (callback) callback(); } //獲取省列表 function getProvinces(callback) { $.ajax({ type: "POST", url: "@Url.Content("~/Backstage/Area/GetProvinces")", data: {}, success: function (text) { $("#province").html(text); if (callback) callback(); }, error: function () { } }); } //獲取市列表 function getCities(pid, callback) { $.ajax({ type: "POST", url: "@Url.Content("~/Backstage/Area/GetCities")", data: { "provinceId": pid, }, success: function (text) { $("#city").html(text); if (callback) callback(); }, error: function () { } }); } //獲取區(qū)縣列表 function getCounties(pid, callback) { $.ajax({ type: "POST", url: "@Url.Content("~/Backstage/Area/GetCounties")", data: { "cityId": pid, }, success: function (text) { $("#county").html(text); if (callback) callback(); }, error: function () { } }); } //獲取商圈列表 function getAreas(pid, callback) { $.ajax({ type: "POST", url: "@Url.Content("~/Backstage/Area/GetAreas")", data: { "countyId": pid, }, success: function (text) { $("#area").html(text); if (callback) callback(); }, error: function () { } }); } </script> </head> <body> <select id="province"> <option value="-1">==請(qǐng)選擇==</option> </select> <select id="city"> <option value="-1">==請(qǐng)選擇==</option> </select> <select id="county"> <option value="-1">==請(qǐng)選擇==</option> </select> <select id="area"> <option value="-1">==請(qǐng)選擇==</option> </select> <script type="text/javascript"> //選擇省 $("#province").change(function () { var id = $(this).find("option:selected").val(); getCities(id, function () { $("#city").change(); }); }); //選擇市 $("#city").change(function () { var id = $(this).find("option:selected").val(); getCounties(id, function () { $("#county").change(); }); }); //選擇區(qū)縣 $("#county").change(function () { var id = $(this).find("option:selected").val(); getAreas(id, function () { $("#area").change(); }); }); getProvinces(function () { select("province", '@provinceId', function () { getCities('@provinceId', function () { select("city", '@cityId', function () { getCounties('@cityId', function () { select("county", '@countyId', function () { getAreas('@countyId', function () { select("area", '@areaId'); }); }); }); }); }); }); }); </script> </body> </html>
Controller代碼:
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Web.Mvc; using DAL; namespace Controllers.Backstage { /// <summary> /// 行政區(qū)劃 /// </summary> public class AreaController : AdminBaseController { #region 構(gòu)造函數(shù)及變量 private SQLiteHelper.SQLiteHelper sqliteHelper; private AreaDal areaDal; public AreaController() { sqliteHelper = new SQLiteHelper.SQLiteHelper(); areaDal = new AreaDal(); } #endregion #region 行政區(qū)劃商圈級(jí)聯(lián)選擇頁面 public ActionResult AreaSelect() { return PartialView(); } #endregion #region 獲取全部省 public ActionResult GetProvinces() { DataTable dt = areaDal.GetProvincesAll(); StringBuilder sbHtml = new StringBuilder(); sbHtml.Append("<option value='-1'>==請(qǐng)選擇==</option>"); foreach (DataRow dr in dt.Rows) { sbHtml.AppendFormat("<option value='{0}'>{1}</option>", dr["id"].ToString(), dr["name"].ToString()); } return Content(sbHtml.ToString()); } #endregion #region 根據(jù)省獲取市 public ActionResult GetCities(string provinceId) { DataTable dt = areaDal.GetCities(provinceId); StringBuilder sbHtml = new StringBuilder(); sbHtml.Append("<option value='-1'>==請(qǐng)選擇==</option>"); foreach (DataRow dr in dt.Rows) { sbHtml.AppendFormat("<option value='{0}'>{1}</option>", dr["id"].ToString(), dr["name"].ToString()); } return Content(sbHtml.ToString()); } #endregion #region 根據(jù)市獲取區(qū)縣 public ActionResult GetCounties(string cityId) { DataTable dt = areaDal.GetCounties(cityId); StringBuilder sbHtml = new StringBuilder(); sbHtml.Append("<option value='-1'>==請(qǐng)選擇==</option>"); foreach (DataRow dr in dt.Rows) { sbHtml.AppendFormat("<option value='{0}'>{1}</option>", dr["id"].ToString(), dr["name"].ToString()); } return Content(sbHtml.ToString()); } #endregion #region 根據(jù)區(qū)縣獲取商圈 public ActionResult GetAreas(string countyId) { DataTable dt = areaDal.GetAreas(countyId); StringBuilder sbHtml = new StringBuilder(); sbHtml.Append("<option value='-1'>==請(qǐng)選擇==</option>"); foreach (DataRow dr in dt.Rows) { sbHtml.AppendFormat("<option value='{0}'>{1}</option>", dr["id"].ToString(), dr["name"].ToString()); } return Content(sbHtml.ToString()); } #endregion } }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- jQuery模擬原生態(tài)App上拉刷新下拉加載更多頁面及原理
- jquery使用iscorll實(shí)現(xiàn)上拉、下拉加載刷新
- JQuery插件iScroll實(shí)現(xiàn)下拉刷新,滾動(dòng)翻頁特效
- jQuery+AJAX實(shí)現(xiàn)無刷新下拉加載更多
- jquery ajax實(shí)現(xiàn)下拉框三級(jí)無刷新聯(lián)動(dòng),且保存保持選中值狀態(tài)
- 用Jquery實(shí)現(xiàn)多級(jí)下拉框無刷新的聯(lián)動(dòng)
- jQuery 翻頁組件yunm.pager.js實(shí)現(xiàn)div局部刷新的思路
- jquery刷新頁面的實(shí)現(xiàn)代碼(局部及全頁面刷新)
- 用Jquery.load載入頁面實(shí)現(xiàn)局部刷新
- jQuery實(shí)現(xiàn)AJAX定時(shí)刷新局部頁面實(shí)例
- JQuery+Ajax無刷新分頁的實(shí)例代碼
- jQuery實(shí)現(xiàn)的上拉刷新功能組件示例
相關(guān)文章
Jquery實(shí)現(xiàn)select multiple左右添加和刪除功能的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄狫query實(shí)現(xiàn)select multiple左右添加和刪除功能的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05jQuery簡(jiǎn)單獲取DIV和A標(biāo)簽元素位置的方法
這篇文章主要介紹了jQuery簡(jiǎn)單獲取DIV和A標(biāo)簽元素位置的方法,結(jié)合實(shí)例形式分析了jQuery針對(duì)頁面div及A標(biāo)簽元素相關(guān)操作技巧,需要的朋友可以參考下2017-02-02jQuery.Sumoselect插件實(shí)現(xiàn)下拉復(fù)選框效果
jquery.sumoselect是一款跨設(shè)備、跨瀏覽器的jQuery下拉列表框插件。接下來通過本文給大家分享jQuery.Sumoselect插件實(shí)現(xiàn)下拉復(fù)選框效果,需要的朋友參考下吧2017-11-11jQuery實(shí)現(xiàn) 注冊(cè)時(shí)選擇閱讀條款 左右移動(dòng)
本篇文章,小編將為大家介紹jQuery實(shí)現(xiàn) 注冊(cè)時(shí)選擇閱讀條款 左右移動(dòng),有需要的朋友可以參考一下2013-04-04正則驗(yàn)證小數(shù)點(diǎn)后面只能有兩位數(shù)的方法
下面小編就為大家?guī)硪黄齽t驗(yàn)證小數(shù)點(diǎn)后面只能有兩位數(shù)的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02使用jquery實(shí)現(xiàn)別踩白塊小游戲的方法實(shí)例
別踩白塊是一款簡(jiǎn)單易上手的數(shù)字小游戲,下面這篇文章主要給大家介紹了關(guān)于使用jquery實(shí)現(xiàn)別踩白塊小游戲的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04jquery如何改變html標(biāo)簽的樣式(兩種實(shí)現(xiàn)方法)
對(duì)于如何修飾html標(biāo)簽,這對(duì)于js來說,可以通過setAttribute來設(shè)置標(biāo)簽的屬性,通過getAttribute來得到標(biāo)簽的屬性,而在jq中當(dāng)然也可以實(shí)現(xiàn)類似的功能,方法上肯定比js要簡(jiǎn)化多了,接下來介紹實(shí)現(xiàn)方法,感興趣的朋友可以了解下2013-01-01