当前位置: 首页 > news >正文

vue2项目 实现上边两个下拉框,下边一个输入框 输入框内显示的值为[“第一个下拉框选中值“ -- “第二个下拉框选中的值“]

效果:

思路:

采用vue中   [computed:] 派生属性的方式实现联动效果,上边两个切换时,下边的跟随变动

demo代码:

<template><div><!-- 第一个下拉框 --><select v-model="firstValue"><option v-for="option in options" :key="option" :value="option">{{ option }}</option></select><!-- 第二个下拉框 --><select v-model="secondValue"><option v-for="option in options" :key="option" :value="option">{{ option }}</option></select><!-- 输入框,显示结果 --><input type="text" :value="computedValue" readonly /></div>
</template><script>
export default {data() {return {firstValue: null,secondValue: null,options: [10, 20, 30, 40, 50], // 假设下拉框选项为数字};},computed: {computedValue() {// 计算输入框中的值,避免出现 NaN 或空值if (this.firstValue !== null && this.secondValue !== null) {return this.firstValue - this.secondValue;}return ''; // 若下拉框未选择,则返回空字符串},},
};
</script>

结合实际应用还可以升级提炼

HTML代码:

<el-form ref="form" :model="form" :rules="rules" label-width="110px"><el-form-item label="第一个下拉框" prop="attributeId"><el-select v-model="form.attributeId" clearable filterable style="width:100%" :disabled="updateStatus == 1" placeholder="请选择"><el-optionv-for="item in bizAttributesList":key="item.id":label="item.attributeName":value="item.id"/></el-select></el-form-item>   <el-form-item label="第二个下拉框" prop="relAttributeId"><el-select v-model="form.relAttributeId" clearable filterable style="width:100%" :disabled="updateStatus == 1" placeholder="请选择"><el-optionv-for="item in bizAttributesList":key="item.id":label="item.attributeName":value="item.id"/></el-select></el-form-item> <el-form-item label="输入框" prop="bizRelDesc"><el-input v-model="form.bizRelDesc" :value="computedValue" disabled/></el-form-item>
</el-form>

结合实际情况基本前两种,获取到的是id累的标志,所以需要在方法中过滤出显示的值

方法代码:

computed: {computedValue() {let attributeIdValue = ''let relAttributeIdValue = ''// 计算输入框中的值,避免出现 NaN 或空值if (this.form.attributeId && this.form.relAttributeId) {//两个都有值this.bizAttributesList.forEach(item=>{if(item.id === this.form.attributeId){attributeIdValue = item.attributeName}if(item.id === this.form.relAttributeId){relAttributeIdValue = item.attributeName}})return this.form.bizRelDesc = `${attributeIdValue}-${relAttributeIdValue}`}else if(this.form.attributeId && !this.form.relAttributeId){//第一个有值 第二个没有this.bizAttributesList.forEach(item=>{if(item.id === this.form.attributeId){attributeIdValue = item.attributeName}})return this.form.bizRelDesc = `${attributeIdValue}-`}else if(!this.form.attributeId && this.form.relAttributeId){//第二个有值 第一个没有this.bizAttributesList.forEach(item=>{if(item.id === this.form.relAttributeId){relAttributeIdValue = item.attributeName}})return this.form.bizRelDesc = `-${relAttributeIdValue}`}return ''; // 若下拉框未选择,则返回空字符串},},

http://www.lryc.cn/news/465867.html

相关文章:

  • el-radio 点击报错 Element with focus: inputAncestor with aria-hidden....
  • 集成平台,互联互通平台,企业大数据平台建设方案,技术方案(Word原件 )
  • 宠物用品交易网站开发:SpringBoot技术详解
  • 解构OpenAI swarm:利用Cursor进行框架分析与示例运行
  • 基于springboot的秦皇岛旅游景点管理系统 设计与实现
  • uniapp展示本地swf格式文件,实现交互
  • ZYNQ:流水灯实验
  • StratoVirt中vCPU拓扑(SMP)配置与实现的深度解析
  • Xml 相关注解使用
  • 本地时间与时区时间转化(以Helpdesk和BPI Challenge 2012为例)
  • Golang | Leetcode Golang题解之第482题秘钥格式化
  • 代码随想录 -- 贪心 -- 无重叠区间
  • sql server xml
  • WPF中MVVM的应用举例
  • 编程题 7-24 约分最简分式【PAT】
  • 尚硅谷大数据Flink1.17实战教程-笔记04【Flink DataStream API】
  • MySQL常见优化策略
  • gyp ERR stack Error: Command failed: D:\python\python.EXE -c import sys; print
  • 代码随想录day6| 242.有效的字母异位词 、349. 两个数组的交集、 202. 快乐数 、 1. 两数之和
  • 《IDE 巧用法宝:使用技巧全解析与优质插件推荐》
  • 安全见闻---清风
  • Python爬虫:urllib_post请求百度翻译(06)
  • GPIO输入和输出
  • 时序动作定位 | DDG-Net:弱监督时间动作定位的判别驱动图网络(ICCV 2023)
  • mapbox没有token/token失效,地图闪烁后变空白,报错Error: A valid Mapbox access token is required to use Mapbox GL JS.
  • C#运动控制
  • 监控易监测对象及指标之:Kafka中间件JMX监控指标解读
  • PDF文件为什么不能编辑是?是啥原因导致的,有何解决方法
  • 海螺AI在人类表情刻画中的应用:技术与创新
  • 【Python实战】几种打包python代码的方法!!!