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 ...
随机推荐
- ResolveUrl的用法
<script src='<%=ResolveUrl("~/UserControls/M3/Validate.js") %>' type="text/j ...
- [notes] some code tips
genericizing-codehtml, body {overflow-x: initial !important;}html { font-size: 14px; } body { margin ...
- MAC版Eclipse的常用快捷键
一.Command类 Command+1 快速修复 Command+d 删除当前行 Command+Option+↓ 复制当前行到下一行 Command+Option+↑ 复制当前行到上一行 Comm ...
- [加密]ESP32 -Secure Boot 安全方案
转自:https://blog.csdn.net/espressif/article/details/79362094 Secure Boot 功能概述 方案概述 Secure Boot 的目的是保证 ...
- 用图片做div背景的列表布局 CSS代码
思路: 1.因为是竖型列表,所以外层用 相对定位,这样 在不会重叠在一起 relative 2.里面用绝对位置 absolute,覆盖在图片上面 3.这里有个小技巧,我里面有个特效,文字竖立,并向右旋 ...
- redis连接超时问题排查
连接池无法获取到连接或获取连接超时redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource f ...
- CentOS 7系统LAMP配置PHP-FPM的示例
CentOS 7 系统出来有一段时间了,今天我们来看在vps中的 CentOS 7 中给lamp环境配置php-fpm的过程,希望文章可以帮助到各位. CentOS 7 已经发布,并且采用 Apach ...
- eclipse如何设置高亮代码的背景色,比如选中某个单词,高亮所有的
设置方法如下: 1.单击IDE顶部Window菜单下的Prefences,如图: 2.在打开对话框的左侧树上,找到Java节点下的Editor中的Mark Occurren,如图: 3.点击Mark ...
- java android使用Gson解析泛型json数据
那就直接开始吧. 在我们获取服务器返回的json数据有时候会出现这种情况,比如: {"body":{"attrName":"feed",&q ...
- Kafka消息的可靠性测试--针对直播业务的方案选择
转自:http://blog.csdn.net/bailove/article/details/44240303 业务场景 来疯直播互动平台,每天有数百万人上下线,有数十万人同时参与互动直播聊天.用户 ...