vue select二级城市联动及第二级默认选中第一个option值
当二级联动比如选择国家的时候,希望选中一个国家的时候后面城市默认选中第一个城市,则给国家的select加一个@change事件就可以了
<div class="inputLine">
<span>所在区域</span>
<select name="" v-model="countryName" @change="selectCountry">
<option :value="item" v-for="(item,index) in area">
{{item.country}}
<svg class="icon icon-arrow-bottom" aria-hidden="true">
<use xlink:href="#icon-arrow-bottom"></use>
</svg>
</option>
</select>
<select name="" v-model="cityName">
<option :value="item" v-for="(item,index) in countryName.city">
{{item}}
<svg class="icon icon-arrow-bottom" aria-hidden="true">
<use xlink:href="#icon-arrow-bottom"></use>
</svg>
</option>
</select>
</div>
data
countryName:{},
cityName:"请选择城市",
area:[
{
"country":"美国",
"city":[
"纽约",
"洛杉矶",
"旧金山",
"西雅图",
"波士顿",
"休斯顿",
"圣地亚哥",
"芝加哥",
"其它",
]
},
{
"country":"加拿大",
"city":[
"温哥华",
"多伦多",
"蒙特利尔",
"其它"
]
},
{
"country":"澳大利亚",
"city":[
"悉尼",
"墨尔本",
"其它"
]
},
{
"country":"新加坡",
"city":[
"新加坡"
]
},
/*{
"country":"中国",
"city":[
"北京市",
]
},*/
],
methods:
selectCountry(value){
this.cityName=this.countryName.city[0];
},
2018.3.13 更新
后来新版本的iview select value的值不支持定义对象形式,所以会报错:
@change事件改为了@on-change 事件
于是换一个实现方法:
<FormItem label="国家" prop="country">
<Row>
<Col span="7">
<Select v-model="formValidate.country" @on-change="selectCountry" placeholder="请选择国家">
<Option v-for="(item,index) in area" :value="item.country" :key="index">{{item.country}}</Option>
</Select>
</Col>
<Col span="17">
</Col>
</Row>
</FormItem>
<FormItem label="城市" prop="city">
<Row>
<Col span="7">
<Select v-model="formValidate.city" placeholder="请选择城市">
<Option v-for="(item,index) in city" :value="item" :key="index">{{item}}</Option>
</Select>
</Col>
<Col span="17">
</Col>
</Row>
</FormItem>
data(){
return {
formValidate: {
country:'',
city:'',
},
area:[
{
"country":"美国",
"city":[
"纽约",
"洛杉矶",
"旧金山",
"西雅图",
"波士顿",
"休斯顿",
"圣地亚哥",
"芝加哥",
"其它",
]
},
{
"country":"加拿大",
"city":[
"温哥华",
"多伦多",
"蒙特利尔",
"其它"
]
},
{
"country":"澳大利亚",
"city":[
"悉尼",
"墨尔本",
"其它"
]
},
{
"country":"新加坡",
"city":[
"新加坡"
]
},
/*{
"country":"中国",
"city":[
"北京市",
]
},*/
],
city:[]
}
}
methods: {
selectCountry(value){
const _this=this;
this.area.forEach(function (item,index) {
if(item.country==value){
_this.city=item.city;
_this.formValidate.city=_this.city[0];
}
})
}
},
新定义一个数组存放被筛选出来的city列表,选择城市的时候遍历这个列表,通过选择国家改变该列表。
vue select二级城市联动及第二级默认选中第一个option值的更多相关文章
- jquery 纯JS设置select下拉框,并默认选中第一个
//html页面<select id="payWay" class="easyui-combobox" name="payWay" s ...
- jQuery设置下拉框select 默认选中第一个option
$("#id option:first").prop("selected", 'selected');
- JS中简单的二级城市联动
代码奉上: <!DOCTYPE html><html><head> <meta charset="UTF-8"> < ...
- VUE 单选下拉框Select中动态加载 默认选中第一个
<lable>分类情况</lable> <select v-model="content.tid"> <option v-for=&quo ...
- Java easyui 下拉框默认选中第一个
html代码: <tr> <td> <div style="margin-bottom:5px">计价方式: <%--下拉框默认选中第 ...
- thymeleaf单选回显,多选回显,选回显,下拉默认选中第一个
//默认选中第一个<input type ="radio" name="repaymentType" th:each ="repaymentTy ...
- pyQT Dialog默认选中某一个选项问题的解决
方法一: 在新建ui文件时不要新建Dialog # -*- coding: utf-8 -*- # Form implementation generated from reading ui file ...
- 老代码:js实现二级城市联动(MVC)
FormViewCity 为mvc控制器传给view的数据,包括一个MyCitys集合字段. <%@ Page Title="" Language="C#" ...
- Vue select默认选中第一个
<td> <select v-model="selectWare"> <option selected="selected" va ...
随机推荐
- RecyclerView 输出的和排版的不一样
遇到过这样一种情况,就是RecyclerView加载出来的和看到Android Studio里的不一样 原因是: @Override public ViewHolder onCreateViewHol ...
- Moving Swiftly(从OC切换到Swift)
Moving Swiftlyhtml, body {overflow-x: initial !important;}html { font-size: 14px; } body { margin: 0 ...
- 字符串过滤掉所有最邻近的“<”和“>”之间的字符
请编写一个方法,实现如下功能:请输入字符串过滤掉所有最邻近的“<”和“>”之间的字符,将其与字符返回. 例如:输入<html><body>4<5<123 ...
- go编译
1. Go编译器 两种官方编译器,gc和gccgo,其中gccgo基于gcc后端. go编译器支持8种指令集,不同建构编译质量不同: amd64 (also known ) (x86 or x86-) ...
- [转] JSON转换
转载自:http://www.360doc.com/content/12/0413/14/9529755_203286509.shtml# JSON简介 JSON(JavaScript Object ...
- RavenDb学习(三)静态索引
在静态索引这块,RavenDb其实的是lucene,所以里面有很多概念,其实都是lucene本身的. .定义静态Indexes documentStore.DatabaseCommands.PutIn ...
- css关于定位那些事情
css绝对定位.相对定位和文档流的那些事 前言 接触html.和css时间也不短了,但每次用div+css布局的时候心里还是有点儿虚,有时候干脆就直接用table算了,很多时候用div会出现些不可预料 ...
- why "Everything" is so fast?
Everything并不扫描整个磁盘,只是读取磁盘上的USN日志,所以速度飞快.但因此缺点也明显:1.只支持NTFS格式的分区,因为USN日志是NTFS专有的.在FAT.FAT32格式分区上无法使用E ...
- 【转】【Python】python使用urlopen/urlretrieve下载文件时出现403 forbidden的解决方法
第一:urlopen出现403 #!/usr/bin/env python # -*- coding: utf- -*- import urllib url = "http://www.go ...
- 解决maven jmxtools 缺失的问题
原因:有版权,所以maven仓库基本都下架了jmxtool. 目前可用的,我就找到了wso2这家,配置如下: <project xmlns="http://maven.apache.o ...