全国省市县三级联动_小程序代码完整版

  • • 发表于 8年前
  • • 作者 易一艺
  • • 3469 人浏览
  • • 2 条评论
  • • 最后编辑时间 8年前
  • • 来自 [技 术]

原创声明:本文为作者原创,未经允许不得转载,经授权转载需注明作者和出处

下载请往:
链接:http://pan.baidu.com/s/1bo17sB1 密码:gxg9

JS:

// pages/index/index.js

var app = getApp();
var areas = app.globalData.areaData.split(“,”);

var provCodes = [];
var cityCodes = [];

Page({
data: {
cueWords: “请选择:”, //提示词

provinces: [],  //省
provIndex: 0,

cities: [], //市
cityIndex: 0,

counties: [], //县
countyIndex: 0,

},

onLoad: function (options) {
this.buildProvince();
},

buildProvince: function () {
var arrProv = [];
provCodes = []; //清空数组
for (var i = 1; i <= areas.length; i++) {
if (areas[i - 1].substring(2, 6) == “0000”) {
var prov_name = areas[i - 1].split(“|”)[1];
var prov_code = areas[i - 1].split(“|”)[0];
arrProv.push(prov_name);
provCodes.push(prov_code);
}
}
this.setData(
{
provinces: arrProv,
});
console.log(this.data.provinces)
},

changeProvince: function (e) {
this.setData({
provIndex: e.detail.value,
});
this.buildCity(provCodes[e.detail.value]);
},

buildCity: function (provinceCode) {
var arrCity = [];
cityCodes = []; //清空数组
var provinceCode = provinceCode / 10000;
for (var i = 1; i <= areas.length; i++) {
if (areas[i - 1].substring(0, 2) == provinceCode
&& areas[i - 1].substring(2, 6) != “0000”
&& areas[i - 1].substring(4, 6) == “00”) {
var city_name = areas[i - 1].split(“|”)[1];
var city_code = areas[i - 1].split(“|”)[0];
arrCity.push(city_name);
cityCodes.push(city_code)
}
}
this.setData(
{
cities: arrCity,
});
},

changeCity: function (e) {
this.setData({
cityIndex: e.detail.value,
});
this.buildCounty(cityCodes[e.detail.value]);
},

buildCounty: function (cityCode) {
var arrCounty = [];
var cityCode = cityCode / 100;
for (var i = 1; i <= areas.length; i++) {
if (areas[i - 1].substring(0, 4) == cityCode
&& areas[i - 1].substring(4, 6) != “00”) {
var county_name = areas[i - 1].split(“|”)[1];
arrCounty.push(county_name);
}
}
this.setData(
{
counties: arrCounty,
});
},

changeCounty: function (e) {
this.setData({
countyIndex: e.detail.value,
});
},

})

分享到:
2条评论
Ctrl+Enter
作者

易一艺

易一艺

APP:1 帖子:1 回复:3 积分:5710

已加入社区[2899]天

主人太懒,签名没设置!

作者详情》
Top