knockout.js 省、市、區三級聯動
首先要引用<script src="~/Scripts/knockout-2.2.0.js"></script>
<script>
$(function () {
Province();
City();
District();
});
function Province() {
viewModel = {
ProvinceCollection: ko.observableArray(),
CityCollection: ko.observableArray(),
DistrictCollection: ko.observableArray()
};
$.ajax({
type: "Post",
url: "/School/GetProvince",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
if (response != "") {
$(response).each(function (index, element) {
viewModel.ProvinceCollection.push(element);
});
ko.applyBindings(viewModel);
$("#Province option[value='@ViewBag.Province']").attr("selected", true);
$("#Province").change();
}
}
});
}
function City() {
$("#Province").change(function () {
var ProvinceId = $("#Province").val().split(',')[0];
$.ajax({
type: "Post",
url: "/School/GetCity",
data: JSON.stringify({
ProvinceID: ProvinceId
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
viewModel.CityCollection.removeAll();
viewModel.DistrictCollection.removeAll();
if (response != "") {
$(response).each(function (index, element) {
viewModel.CityCollection.push(element);
});
ko.applyBindings(viewModel);
$("#City option[value='@ViewBag.City']").attr("selected", true);
$("#City").change();
}
}
});
});
}
function District() {
$("#City").change(function () {
var CityId = $("#City").val().split(',')[0];
var ProvinceId = $("#Province").val().split(',')[0];
$.ajax({
type: "Post",
url: "/School/GetDistrict",
data: JSON.stringify({
ProvinceID: ProvinceId,
CityID: CityId
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
viewModel.DistrictCollection.removeAll();
if (response != "") {
$(response).each(function (index, element) {
viewModel.DistrictCollection.push(element);
});
ko.applyBindings(viewModel);
$("#District option[value='@ViewBag.District']").attr("selected", true);
}
}
});
});
}
</script>
<div class="form-group">
<label class="col-sm-2 control-label">省份</label>
<div class="col-sm-2">
<select class="form-control " required="" data-bind="options: ProvinceCollection, optionsCaption: '請選擇省份...',
optionsValue: function (item) { return item.Value; },
optionsText: function (item) { return item.Text; }, value: Province,
valueUpdate: 'change'" id="Province" ng-model="FormData.Province" name="Province"></select>
<input type="hidden" name="Province" />
</div>
<label class="col-sm-1 control-label">城市</label>
<div class="col-sm-2">
<select class="form-control " required="" data-bind="options: CityCollection, optionsCaption: '請選擇城市...',
optionsValue: function (item) { return item.Value; },
optionsText: function (item) { return item.Text; }, value: City,
valueUpdate: 'change'" id="City" ng-model="FormData.City" name="City"></select>
<input type="hidden" name="City" />
</div>
<label class="col-sm-1 control-label">區縣</label>
<div class="col-sm-2">
<select class="form-control " required="" data-bind="options: DistrictCollection, optionsCaption: '請選擇區縣...',
optionsValue: function (item) { return item.Value; },
optionsText: function (item) { return item.Text; }, value: District,
valueUpdate: 'change'" id="District" ng-model="FormData.District" name="District"></select>
<input type="hidden" name="District" />
</div>
</div>
//三級聯動中的省份
[HttpPost]
public JsonResult GetProvince()
{
var province = AdministrativeCode.LstAdministrativeCode;
var lstitem2 = new List<SelectListItem>();
var Pli = new SelectListItem();
foreach (var item in province)
{
Pli = new SelectListItem();
Pli.Text = item.Caption;
Pli.Value = item.ID.ToString() + "," + item.Caption;
lstitem2.Add(Pli);
}
return Json(lstitem2);
}
//三級聯動中的城市
[HttpPost]
public JsonResult GetCity(int ProvinceID)
{
var CityList = AdministrativeCode.LstAdministrativeCode.Where(o => o.ID == ProvinceID).ToEntity().SubAdministrativeCode;
var lstitem2 = new List<SelectListItem>();
var Pli = new SelectListItem();
foreach (var item in CityList)
{
Pli = new SelectListItem();
Pli.Text = item.Caption;
Pli.Value = item.ID.ToString() + "," + item.Caption;
lstitem2.Add(Pli);
}
return Json(lstitem2);
}
[HttpPost]
public JsonResult GetDistrict(int ProvinceID, int CityID)
{
var DistrictList = AdministrativeCode.LstAdministrativeCode.Where(o => o.ID ==ProvinceID).ToEntity().SubAdministrativeCode.Where(o=>o.ID==CityID).ToEntity().SubAdministrativeCode;
var lstitem2 = new List<SelectListItem>();
var Pli = new SelectListItem();
foreach (var item in DistrictList)
{
Pli = new SelectListItem();
Pli.Text = item.Caption;
Pli.Value = item.ID.ToString() + "," + item.Caption;
lstitem2.Add(Pli);
}
return Json(lstitem2);
}
相關文章
- vue 省市區三級聯動外掛Vue
- 三級聯動
- 基於layui的省市區三級聯動(資料互動)UI
- 符合 iview 資料規則的省市區三級聯動View
- 原生javascript製作省市區三級聯動詳細教程JavaScript
- Laravel-admin 三級聯動 or 多級聯動 編輯 and 新建Laravel
- 小程式 三級地址聯動
- 省市縣三級聯動(2)
- 省市縣三級聯動(1)
- 用Vue、element-ui、axios實現省市區三級聯動VueUIiOS
- 04-python3.5-模擬三級選單-省-縣-區域–01Python
- 2020年中國全國5級行政區劃(省、市、縣、鎮、村)
- [Dcat 擴充套件] dcat-distpicker 省市區三級聯動選擇元件擴充套件元件
- 中國省市區街道四級聯動地區選擇元件元件
- 用python實現列印省、市、縣三級選單 可返回上一級 可隨時退出程式Python
- [外掛擴充套件]三級聯動外掛!!!!!!!!套件
- 若依框架——>三級聯動 新增,預設修改框架
- 快訊丨NOEL諾爾亞太區,東北三省全面啟動
- 使用ElementUI和element-china-area-data庫實現省市區三級聯動元件封裝UI元件封裝
- 獲取行政區劃資訊(省、市)工具類
- Flutter自定義View——仿高德三級聯動DrawerFlutterView
- Vantui---picker三級聯動設定預設值UI
- 小程式:動態資料實現三級聯動選擇器
- UIPikerView 省市區三聯滑動,解決滑動崩潰問題UIView
- js二級聯動JS
- MVC的二級聯動MVC
- 省選聯考 2024
- js 實現二級聯動JS
- 小資料二級聯動-封裝成一個二級聯動部分檢視封裝
- 等保二級和等保三級的三大區別講解
- 零碳錄:省級氣候行動進展概覽
- 微信小程式--手寫一個地區選擇器(多級聯動)微信小程式
- 最全最新??中國【省、市、區縣、鄉鎮街道】json,csv,sql資料JSONSQL
- 利用Bootstrap Paginator外掛和knockout.jsbootJS
- 聯農會:聯動市場一心為農
- 原生JS實現二級聯動JS
- 2024聯合省選遊記
- [聯合省選 2024] 季風