*初级小白*~ 2022-12-05 18:22 采纳率: 97.9%
浏览 26
已结题

antdesign的下拉框

vue3 使用antdesign的下拉框,如何既可从下拉列表选择,又可输入手动输入文本(既可下拉选择又可自己输入)

<template>
  <div>{{ value1 }}</div>
  <h2>use a-select-option</h2>
  <a-space>
    <a-select ref="select" v-model:value="value1" style="width: 120px" :show-search="true" @focus="focus"
      @change="handleChange" placeholder="请输入或者选择">
      <a-select-option :value="item" v-for="(item, index) in data">{{ item }}</a-select-option>
    </a-select>
  </a-space>
</template>
<script>
import { defineComponent, ref } from 'vue';
export default defineComponent({
  setup() {
    const data = ['Jack', 'Lucy', 'Disabled', 'yiminghe'];
    const value1=ref(null);
    const focus = () => {
      console.log('focus');
    };
    const handleChange = value => {
      console.log(`selected ${value}`);
    };
    return {
      focus,
      handleChange,
      value1,
      data,
    };
  },
});
</script>


  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2022-12-05 18:38
    关注

    需要对输入框事件进行处理,oninput记录输入值,onblur时设置model的值。示例如下

    <template>
      <div>{{ value1 }}</div>
      <h2>use a-select-option</h2>
      <a-space>
        <a-select ref="select" v-model:value="value1" style="width: 120px" :show-search="true" @focus="focus"
          @change="handleChange" placeholder="请输入或者选择">
          <a-select-option :value="item" v-for="(item, index) in data">{{ item }}</a-select-option>
        </a-select>
      </a-space>
    </template>
    <style>
    .ant-select-dropdown-empty,.ant-select-item-empty{display:none!important}
    </style>
    <script>
    import { defineComponent, ref } from 'vue';
    export default defineComponent({
      setup() {
        const data = ['Jack', 'Lucy', 'Disabled', 'yiminghe'];
        const value1=ref(null);
        let init=false;
        let inputValue=ref(null);
        const focus = (e) => {
          if(!init){
            e.target.oninput=function(){
              inputValue.value=this.value
            };
            e.target.onblur=function(){
              value1.value=inputValue.value
            };
            init=true;
          }
        };
        const handleChange = value => {
          inputValue.value=value
        };
        return {
          focus,
          handleChange,
          value1,
          data,
        };
      },
    });
    </script>
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 12月17日
  • 已采纳回答 12月9日
  • 创建了问题 12月5日

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题