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

element里的select自定义输入的时候,不用点击下拉框选中自定义输入,而是当焦点失去的时候自动赋值输入的内容

在Element UI的el-select中,可以通过自定义失焦事件处理来实现当焦点离开时自动将输入内容赋值给选择框。

实现思路

  1. 使用el-select的filterableallow-create属性允许自定义输入

  2. 添加自定义输入框(如el-input)用于接收用户输入

  3. 监听输入框的blur事件,在失焦时将输入内容添加到选择框

代码实现

<el-selectv-model="queryParams.receiverName"placeholder="请选择收货人"clearablefilterableallow-create@blur="receiverBlur"
><el-option v-for="(item,index) in contactInformation" :key="index" :label="item.receiverName" :value="item.id" />
</el-select>
const receiverBlur = (event) => {if(!event.target.value){return}if((!queryParams.value.receiverName && event.target.value) || (queryParams.value.receiverName != event.target.value)){queryParams.value.receiverName = event.target.value}
}

完整代码

<!DOCTYPE html>
<html lang="zh-CN">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>el-select失焦自动赋值</title><link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css"><style>body {font-family: 'Helvetica Neue', Arial, sans-serif;padding: 20px;background-color: #f5f7fa;}.container {max-width: 600px;margin: 0 auto;background: white;padding: 25px;border-radius: 8px;box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);}.demo-title {text-align: center;margin-bottom: 25px;color: #409EFF;font-weight: 500;}.select-container {margin-bottom: 20px;}.custom-input {margin-top: 15px;}.tip {font-size: 13px;color: #909399;margin-top: 8px;}</style>
</head>
<body><div id="app" class="container"><h2 class="demo-title">el-select失焦自动赋值示例</h2><div class="select-container"><el-select v-model="selectedValue" filterable allow-create placeholder="请选择或输入内容"style="width: 100%"@blur="handleBlur"><el-optionv-for="item in options":key="item.value":label="item.label":value="item.value"></el-option></el-select><p class="tip">直接在输入框输入后,失去焦点即可自动添加</p></div><div class="custom-input"><el-inputv-model="customInput"placeholder="或在此输入后失焦自动添加到选择框"@blur="addCustomOption"style="width: 100%"><template slot="append"><el-button icon="el-icon-plus" @click="addCustomOption"></el-button></template></el-input></div><div style="margin-top: 25px;"><p>当前选中值: <strong>{{ selectedValue }}</strong></p><p>选项列表: {{ options.map(o => o.label) }}</p></div></div><script src="https://cdn.jsdelivr.net/npm/vue@2.6.14/dist/vue.js"></script><script src="https://unpkg.com/element-ui/lib/index.js"></script><script>new Vue({el: '#app',data() {return {selectedValue: '',customInput: '',options: [{ value: 'option1', label: '选项1' },{ value: 'option2', label: '选项2' },{ value: 'option3', label: '选项3' },{ value: 'option4', label: '选项4' },{ value: 'option5', label: '选项5' }]};},methods: {// 处理el-select的失焦事件handleBlur(event) {const inputValue = event.target.value;if (inputValue && !this.options.some(opt => opt.label === inputValue)) {this.addNewOption(inputValue);}},// 添加自定义输入选项addCustomOption() {if (this.customInput && !this.options.some(opt => opt.label === this.customInput)) {this.addNewOption(this.customInput);this.customInput = '';}},// 添加新选项addNewOption(label) {const newOption = {value: 'custom_' + Date.now(),label: label};this.options.push(newOption);this.selectedValue = newOption.value;this.$message({message: `已添加选项: ${label}`,type: 'success',duration: 2000});}}});</script>
</body>
</html>

功能说明

  1. 上方el-select支持直接输入内容,失焦后自动添加为新选项

  2. 下方输入框可以输入自定义内容,失焦或点击右侧"+"按钮添加为新选项

  3. 添加新选项后会自动选中该选项并显示成功消息

注意事项

  • 需要添加filterableallow-create属性使el-select支持自定义输入

  • 通过监听blur事件捕获输入内容

  • 需要检查输入内容是否已存在,避免重复添加

  • 添加新选项后自动选中该选项提供更好的用户体验

http://www.dtcms.com/a/395873.html

相关文章:

  • 链改2.0+港促会,携手赋能 Web3引企赴港!
  • C++第二篇:命名空间(namespace)
  • vcsa 重启服务
  • QT 两种库写法 LIBS += .a和LIBS += -L -l
  • 比斯特自动化|电动自行车电池点焊机的作用与使用
  • Django 模型与 ORM 全解析(一):从基础到实战的完整指南
  • NW955NW960美光固态闪存NW963NW971
  • iOS 26 软件兼容性大检查,哪些 App 出问题、API 变动要注意、旧功能不支持兼容性测试全流程
  • HarmonyOS NEXT互动卡片开发:从原理到实战的完整指南
  • 邪修实战系列(6)
  • Clover: 1靶场渗透
  • 智慧供水管网监测解决方案:实现压力、流量、水质数据集与监控
  • 深入理解Java虚拟机内存模型
  • 什么是缺陷检测?机器视觉表面缺陷检测从定义到实战方法,避开漏判误判
  • Svelte:编译时优化原理、与传统虚拟DOM框架的性能对比性能优化
  • 属性描述符
  • JavaWeb之JSP 快递管理与过滤器详解
  • 《MedChat智能医疗问答系统》项目介绍
  • 使用FastAPI和Docker部署机器学习模型:从开发到生产的最佳实践
  • Per-Tensor 量化和Per-Channel 量化
  • 执行bat任务栏有图标显示,执行pycharm64.exe就没有是什么原因
  • 【Docker项目实战】使用Docker部署wealth-tracker个人资产分析工具
  • LeapMotion_Demo演示
  • 智慧图书管理|基于SprinBoot+vue的智慧图书管理系统(源码+数据库+文档)
  • 面试技巧第四篇:嵌入式通信机制考点:消息队列、信号量与互斥锁
  • 面试八股:C语言的预处理和类型定义
  • 强化学习1.3 深度学习交叉熵方法
  • 用PowerBI的思想解决QuickBI文本无法动态配色问题
  • 逆向解析 1688 商品详情接口:自主构建 Sign 签名算法实战
  • SpringCloud项目阶段六:feign服务降级处理以及基于DFA算法的自管理敏感词审核和tess4j图片文字识别集成