当二级联动比如选择国家的时候,希望选中一个国家的时候后面城市默认选中第一个城市,则给国家的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. java框架篇---Struts入门

    首先理解Struts与MVC的关系 在传统的MVC模式中所有的请求都要先交给Servlet处理,之后由Servlet调用JavaBean,并将结果交给JSP中进行显示.结构图如下 Struts是Apa ...

  2. web前端开发与iOS终端开发的异同[转]

    * {-webkit-tap-highlight-color: rgba(0,0,0,0);}html {-webkit-text-size-adjust: none;}body {font-fami ...

  3. html超链接,锚点以及特殊字符

    超链接 <a></a>中不加东西是显示不了的. href:跳转的地址 target:_self(本页面打开,默认选项),_blank(新页面打开) title:文本提示 空链接 ...

  4. mongo源码学习(一)

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

  5. TP v5中Request取值方式变化

    到目前为止的5.0.7版本中,route里相关参数不会再压入$_GET与$_REQUEST变量中,比如 index.php/user/blog/id/123 里我们想用 $_GET['id']是取不到 ...

  6. Android View.MeasureSpec

    有时,Android系统控件无法满足我们的需求,因此有必要自定义View. 一般来说,自定义控件都会去重写View的onMeasure方法,因为该方法指定该控件在屏幕上的大小,[protected v ...

  7. AR2220 通过cpu-defend policy处理大量大量arp广播的小技巧

    今天发现有局域网里面有几台电脑中毒了,在大量的发送ARP报文,导致设备cpu利用率很高. 一.查看display cpu-defend statistics 发现arp-reply  arp-requ ...

  8. Extjs4.2 Grid搜索Ext.ux.grid.feature.Searching的使用

    背景 Extjs4.2 默认提供的Search搜索,功能还是非常强大的,只是对于国内的用户来说,还是不习惯在每列里面单击好几下再筛选,于是相当当初2.2里面的搜索,更加的实用点,于是在4.2里面实现. ...

  9. 安装Nginx+Lua+OpenResty开发环境配置全过程实例

    安装Nginx+Lua+OpenResty开发环境配置全过程实例 OpenResty由Nginx核心加很多第三方模块组成,默认集成了Lua开发环境,使得Nginx可以作为一个Web Server使用. ...

  10. SAP 以工序为基准进行发料 机加工行业 Goods Issue to Routing

    SAP 以工序为基准进行发料   这个流程是在业务有关需求,业务需要按照工序发料,一个工单有多个工序,而料是要发到每个工序上,而且没到工序之间在物理上是有距离的,所以仓管员在打印配发单之后希望了解到哪 ...