当二级联动比如选择国家的时候,希望选中一个国家的时候后面城市默认选中第一个城市,则给国家的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值的更多相关文章

  1. jquery 纯JS设置select下拉框,并默认选中第一个

    //html页面<select id="payWay" class="easyui-combobox" name="payWay" s ...

  2. jQuery设置下拉框select 默认选中第一个option

    $("#id option:first").prop("selected", 'selected');

  3. JS中简单的二级城市联动

    代码奉上: <!DOCTYPE html><html><head>    <meta charset="UTF-8">    < ...

  4. VUE 单选下拉框Select中动态加载 默认选中第一个

    <lable>分类情况</lable> <select v-model="content.tid"> <option v-for=&quo ...

  5. Java easyui 下拉框默认选中第一个

    html代码: <tr> <td> <div style="margin-bottom:5px">计价方式:   <%--下拉框默认选中第 ...

  6. thymeleaf单选回显,多选回显,选回显,下拉默认选中第一个

    //默认选中第一个<input type ="radio" name="repaymentType" th:each ="repaymentTy ...

  7. pyQT Dialog默认选中某一个选项问题的解决

    方法一: 在新建ui文件时不要新建Dialog # -*- coding: utf-8 -*- # Form implementation generated from reading ui file ...

  8. 老代码:js实现二级城市联动(MVC)

    FormViewCity 为mvc控制器传给view的数据,包括一个MyCitys集合字段. <%@ Page Title="" Language="C#" ...

  9. Vue select默认选中第一个

    <td> <select v-model="selectWare"> <option selected="selected" va ...

随机推荐

  1. mongo源码学习(一)

    在git上把mongo的源码给拉下来了,然后目录大概是这样的: 这个mongo是用C++写的,编译并没有用Makefile而是用的scons工具,这个好像是python写的. mongo后台进程的入口 ...

  2. MT29F8G08ABACA nandflash 参数记录

    跟踪 MT29F8G08ABACA datasheet ,记录里面有关软件的参数 首先,硬件上的特征,page size , oob , block size [](http://images2017 ...

  3. [Timer]应用层实现sleep

    转自:https://www.cnblogs.com/longbiao831/p/4556246.html Select只能做延时,可以做回调吗? 本文讲述如何使用select实现超级时钟.使用sel ...

  4. Using the SAMA5D2-compatible ADC device

    Introduction Kernel Software triggers Unsigned single-ended channel conversion Setup Test procedure ...

  5. Android 一条竖线或横线、画边框

    Android 一条竖线或横线.画边框 博客分类: android androidshape  1.画线 [img] http://pic002.cnblogs.com/images/2010/122 ...

  6. [转]Gson的基本使用

    gson和其他现有java json类库最大的不同时gson需要序列化得实体类不需要使用annotation来标识需要序列化得字段,同时gson又可以通过使用annotation来灵活配置需要序列化的 ...

  7. 聊聊Google face api

    图像处理开源了很多东西,保存下一些基础的东西,以用来follow最新的东西. Google Face API 是什么? Google 的 Face API 用于面部检测,从图片中找出人的面部,以及位置 ...

  8. Java 源码赏析 - java.lang - Void

    被人鄙视了,于是也来读读源码... package java.lang; /** * The Void class is an uninstantiable placeholder class to ...

  9. HttpWebRequest、HttpWebResponse获取网页

    1,通过HttpWebRequest.HttpWebResponse获取一个流 request = (HttpWebRequest)System.Net.WebRequest.Create(this. ...

  10. ELK(Logstash+Elasticsearch+Kibana)的原理和详细搭建

    一. Elastic Stack Elastic Stack是ELK的官方称呼,网址:https://www.elastic.co/cn/products ,其作用是“构建在开源基础之上, Elast ...