基于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
}
}
以上就是本文的全部?jī)?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)的左右移動(dòng)焦點(diǎn)圖效果
這篇文章主要介紹了jQuery實(shí)現(xiàn)的左右移動(dòng)焦點(diǎn)圖效果,涉及jQuery結(jié)合時(shí)間函數(shù)響應(yīng)鼠標(biāo)事件動(dòng)態(tài)操作頁面元素樣式的相關(guān)技巧,需要的朋友可以參考下2016-01-01
JQueryMiniUI按照時(shí)間進(jìn)行查詢的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄狫QueryMiniUI按照時(shí)間進(jìn)行查詢的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
jQuery分頁插件jquery.pagination.js使用方法解析
這篇文章主要為大家詳細(xì)解析了jQuery分頁插件jquery.pagination使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
dul無法加載bootstrap實(shí)現(xiàn)unload table/user恢復(fù)
這篇文章主要介紹了dul無法加載bootstrap實(shí)現(xiàn)unload table/user恢復(fù)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
jQuery插件JWPlayer視頻播放器用法實(shí)例分析
這篇文章主要介紹了jQuery插件JWPlayer視頻播放器用法,結(jié)合實(shí)例形式分析了JWPlayer插件播放視頻的相關(guān)操作技巧,需要的朋友可以參考下2017-01-01
jQuery 實(shí)現(xiàn)鼠標(biāo)畫框并對(duì)框內(nèi)數(shù)據(jù)選中的實(shí)例代碼
本文通過實(shí)例代碼給大家介紹了jQuery 實(shí)現(xiàn)鼠標(biāo)畫框并對(duì)框內(nèi)數(shù)據(jù)選中的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-08-08

