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 ...
随机推荐
- 项目抛弃Tomcat容器,用代码启动Tomcat插件
tomato启动代码如下: package tomcat; import org.apache.catalina.connector.Connector; import org.apache.cata ...
- AutoLayout——何为intrinsic content size
上一篇说到了约束就是等式和不等式.仅仅知道其原理还是没法拉出符合需求的线.所以这一篇主要看来什么是intrinsic content size,以及它有什么用. 在Xcode中,差点儿全部控件或视图, ...
- C++实现的服务不可用发送报警邮件
正在看C++,我主业是Java开发,但是最近服务不太稳定,自己用C++造了个小轮子. 配置好要监控的访问路径,IP.端口号和路径.涉及到的知识点有以下几个部分: 构造函数与析构函数 内存的分配与释放( ...
- mysql找到所有索引
SELECT a.TABLE_SCHEMA,a.TABLE_NAME,a.index_name,GROUP_CONCAT(column_name ORDER BY seq_in_index) AS ` ...
- QueenPuzzle-N皇后问题
详见-算法之美-p180. #include <iostream> #include <memory.h> #include <conio.h> #include ...
- 卡片式ViewPager,一屏展示多个pager item,设置高度不一致的tabBar
ViewPager的基本用法不必多说,这都很简单,我们可以在ViewPager中加载一个ImageView,也可以加载一个Fragment,这都是目前非常常见的用法.那么我今天说的是ViewPager ...
- Android跳转系统界面_大全集
1.跳转Setting应用列表(所有应用) Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS); ...
- C++ operator关键字详解
C++中的operator主要有两个作用,一是操作符的重载,一是自定义对象类型的隐式转换. 类型转换操作符(type conversion operator)是一种特殊的类成员函数,它定义将类类型值转 ...
- Python 文件操作三
文件重命名os模块中的rename()可以完成对文件的重命名操作rename(需要修改的文件名, 新的文件名) import os os.rename("11.txt", &quo ...
- 深入浅出 kvm qemu libvirt
在所谓的kvm技术中,应用到的其实有2个东西:qemu+kvmkvm负责cpu虚拟化+内存虚拟化,实现了cpu和内存的虚拟化,但kvm不能模拟其他设备:qemu是模拟IO设备(网卡,磁盘),kvm加上 ...