地区选择三级联动

新手,新手,新手,写的很基础,很基础,会的有限,做项目是要用到,但是没有合适的插件,自己写一个

先看效果,

 当点击每一层级的时候都还有相应的样式变化

html部分:

<div class="eject-area" v-if="visiblearea">
        <div class="major-area">
            <div class="header">
                <div class="province" style="width: 34%;" :class="{addStyle: bordeRight}">省</div>
                <div class="city" :class="{addStyle: borderBottom}">市</div>
                <div class="region" :class="{addStyle: borderTop}">地区</div>
            </div>
            <div class="alldata">
                <div class="province" style="width: 22%;background-color: #f4f6fa;" @click="allProvice">全部</div>
                <div class="city" @click="allCity">全部</div>
                <div class="region" @click="allRegin">全部</div>
            </div>
            <div class="content-area">
                <div class="province">
                    <div class="provice-data" v-for="(item, index) of provicelist" @click="addClass(item,index)" :class="{selected: index == current}">{{item.provinceName}}</div>
                </div>
                <div class="city">
                    <div class="city-data" v-for="(item, index) of citylist" @click="addClassTwo(item,index)" :class="{selected: index == currenttwo}">{{item.cityName}}</div>
                </div>
                <div class="region">
                    <div class="region-data" v-for="(item, index) of regionlist" @click="addClassThree(item,index)" :class="{selected: index == currentthree}">{{item.districtName}}</div>
                </div>
            </div>
        </div>
    </div>

css部分:

.eject-area {
		width: 100%;
		background: #FFFFFF;
		margin: 0.2rem auto;
		position: absolute;
		top: 1.2rem;
		left: 0;
		z-index: 10000;
		
		.major-area {
			margin-top: 0.2rem;
			font-size: 0.4rem;
			border-top: 3px solid #f0f3f7;
			.header {
				width: 74%;
				height: 1rem;
				display: flex;
				// border: 1px solid red;
				div {
					width: 40%;
					height: 1rem;
					text-align: center;
					line-height: 1rem;
					margin: 0 12% 0 8%;
				}
			}
			.alldata {
				width: 100%;
				display: flex;
				border-top: 1px solid #C3DCFF;
				div {
					width: 25%;
					height: 1rem;
					text-align: center;
					line-height: 1rem;
				}
			}
			.content-area {
				width: 74%;
				height: 100%;
				display: flex;
				.province {
					width: 30%;
					background-color: #f4f6fa;
					margin-bottom: 1rem;
					div {
						height: 1.2rem;
						text-align: center;
						line-height: 1.2rem;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}
				}
				.city {
					width: 33.5%;
					div {
						height: 1.2rem;
						text-align: center;
						line-height: 1.2rem;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}
				}
				.region {
					width: 33.5%;
					div {
						height: 1.2rem;
						text-align: center;
						line-height: 1.2rem;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
					}
				}
			}
			.addStyle {
				border-bottom: 2px solid #0b7ee8;
				color: #0b7ee8;
			}
			.selected {
				background-color: #fff;
				color: #0b7ee8;
			}
		}
	}

js部分:

export default {
  name: "BaseHeader",
  components: {
  },
  data() {
    return {
      // 地区选择
      visiblearea: false,
      // 文本框内容
      inputValue: "",
      // 列表内容
      provicelist: "",
      citylist: "",
      regionlist: "",
      // 点击样式
      current: null,
      currenttwo: null,
      currentthree: null,
      borderBottom: 1,
      borderTop: 0,
      bordeRight: 0,
      isView: true,
      ofValue: "",
      onValue: "",
      inValue: "",
      // 回传ID
      regionID: "",
    };
  },
  computed: {

  },
  created() {
    this.allListProvice();
  },
  mounted() {
  },
  methods: {
    // 拼接地区内容
    splicing() {
        this.inputValue = this.ofValue + this.onValue + this.inValue;
    },
    // 获取所有省列表
    allListProvice() {
        PROVICE_LIST()
            .then((res) => {
                // console.log(res);
                this.provicelist = res.data;
            })
            .catch((err) => {
                console.log(err);
            })
    },
    // 点击样式
    addClass(item,index) {
        CITY_LIST(item.provinceCode)
            .then((res) => {
                // console.log(res);
                this.citylist = res.data;
                this.regionlist = undefined;
            })
            .catch((err) => {
                console.log(err);
            })
        this.current = index;
        this.allCity();
        this.bordeRight = 1;
        this.ofValue = item.provinceName;
        this.splicing();
        this.isView = false;
    },
    addClassTwo(item,index) {
        REGION_LIST(item.cityCode)
            .then((res) => {
                // console.log(res);
                this.regionlist = res.data;
            })
            .catch((err) => {
                console.log(err);
            })
        this.currenttwo = index;
        this.currentthree = null;
        this.borderBottom = 1;
        this.borderTop = 0;
        this.bordeRight = 0;
        this.onValue = item.cityName;
        this.splicing();
    },
    addClassThree(item,index) {
        this.currentthree = index;
        this.borderBottom = 0;
        this.borderTop = 1;
        this.bordeRight = 0;
        this.inValue = item.districtName;
        this.splicing();
        this.regionID = item.districtCode;
    },
    // 点击全部
    allProvice() {
        this.allCity();
        this.current = null;
        this.bordeRight = 0;
        this.ofValue = "";
        this.splicing();
        // this.provicelist = undefined;
    },
    allCity() {
        this.currenttwo = null;
        this.allRegin();
        this.borderBottom = null;
        this.bordeRight = 1;
        this.citylist = undefined;
        this.onValue = "";
        this.splicing();
    },
    allRegin() {
        this.currentthree = null;
        this.borderTop = null;
        this.borderBottom = 1;
        this.bordeRight = 0;
        this.regionlist = undefined;
        this.inValue = "";
        this.splicing();
    },
  }
};

另外还有上边的input输入框 可以当点击内容时自动拼接,

当点击全部时会自动清空当前级联的内容和样式。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的HTML三级联动地区选择框的示例代码: ```html <!DOCTYPE html> <html> <head> <title>三级联动地区选择框</title> <meta charset="utf-8"> <script type="text/javascript"> window.onload=function(){ var province=document.getElementById("province"); var city=document.getElementById("city"); var district=document.getElementById("district"); var provinceArr=["请选择省份","北京","上海","天津"]; var cityArr=[["请选择城市"],["海淀区","朝阳区","东城区"],["浦东新区","徐汇区","长宁区"],["和平区","河西区","河东区"]]; var districtArr=[[["请选择区/县"]],[["海淀区1","海淀区2","海淀区3"],["朝阳区1","朝阳区2","朝阳区3"],["东城区1","东城区2","东城区3"]],[["浦东新区1","浦东新区2","浦东新区3"],["徐汇区1","徐汇区2","徐汇区3"],["长宁区1","长宁区2","长宁区3"]],[["和平区1","和平区2","和平区3"],["河西区1","河西区2","河西区3"],["河东区1","河东区2","河东区3"]]]; for(var i=0;i<provinceArr.length;i++){ var option=document.createElement("option"); option.value=i; option.innerHTML=provinceArr[i]; province.appendChild(option); } province.onchange=function(){ city.length=1; district.length=1; if(this.value==0) return; for(var j=0;j<cityArr[this.value].length;j++){ var option=document.createElement("option"); option.value=j; option.innerHTML=cityArr[this.value][j]; city.appendChild(option); } } city.onchange=function(){ district.length=1; if(this.value==0) return; for(var k=0;k<districtArr[province.value][this.value].length;k++){ var option=document.createElement("option"); option.value=k; option.innerHTML=districtArr[province.value][this.value][k]; district.appendChild(option); } } } </script> </head> <body> <label>省份:</label> <select id="province"> <option value="0">请选择省份</option> </select> <label>城市:</label> <select id="city"> <option value="0">请选择城市</option> </select> <label>区/县:</label> <select id="district"> <option value="0">请选择区/县</option> </select> </body> </html> ``` 在这个示例中,我们定义了三个下拉选择框`province`、`city`和`district`,分别表示省份、城市和区/县。然后,我们通过JavaScript动态生成了三个数组`provinceArr`、`cityArr`和`districtArr`,用于存储省份、城市和区/县的选项,这些选项将在下拉选择框中显示。最后,我们通过`onchange`事件监听器来实现下拉选择框之间的联动。当省份选择框的值改变时,城市和区/县选择框中的选项也会随之改变。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值