vue 自定义下拉搜索框

vue 自定义下拉搜索框

直接上图看效果
在这里插入图片描述

上代码

<template>
  <div>
    <div style="margin-top: 15px; width: 80%">
      <el-input
        placeholder="请输入内容"
        v-model="searchValue"
        class="input-with-select"
        clearable
        ref="input"
        @focus="onInputFocus"
        @blur="onInputBlur"
        @input="onInputFocus"
      >
        <el-select v-model="select" slot="prepend" placeholder="热舞广泛的给">
          <el-option label="餐厅名" value="1"></el-option>
          <el-option label="订单号" value="2"></el-option>
          <el-option label="用户电话" value="3"></el-option>
        </el-select>
        <el-button slot="append" icon="el-icon-search">搜索</el-button>
      </el-input>
      <div class="selset_wrap" v-if="selsetWrapShow">
        <div class="selset_wrap_title">历史记录</div>
        <div
          class="selset_wrap_list"
          v-for="(item, index) in inputData"
          :key="index"
        >
          <div class="selset_wrap_list_list">
            <div
              class="selset_wrap_list_list_name"
              @mousedown.prevent="searchValueFn(item)"
            >
              {{ item.name }}
            </div>
            <div
              class="selset_wrap_list_list_remove"
              @mousedown.prevent="removeFn(index)"
            >
              <i class="el-icon-delete"></i>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchValue: "",
      select: "餐厅名",
      restaurants: [],
      state: "",
      timeout: null,
      selsetWrapShow: false,
      inputData: [
        { name: "在知识的山峰上登得越高,眼前的景色越壮阔", id: "1" },
        { name: "万事皆由人的意志创造。", id: "2" },
        { name: "万般皆是命,半点不由人", id: "3" },
        { name: "身外障碍事小,心中障碍事大。", id: "4" },
        {
          name: "实力的来源不是胜利。唯有奋斗才能增强实力。当你历经苦难而不气馁,那就是实力。唯有奋斗才能增强实力。当你历经苦难而不气馁,那就是实力唯有奋斗才能增强实力。当你历经苦难而不气馁,那就是实力",
          id: "5",
        },
        {
          name: "实力的来源不是胜利。唯有奋斗才能增强实力。当你历经苦难而不气馁,那就是实力。",
          id: "6",
        },
      ],
    };
  },
  methods: {
    onInputFocus() {
      this.selsetWrapShow = true;
      console.log("获取焦点");
    },
    onInputBlur() {
      this.selsetWrapShow = false;
      console.log("失去焦点");
    },
    searchValueFn(val) {
      this.selsetWrapShow = false;
      this.searchValue = val.name;
      this.$refs["input"].blur();
      console.log(this.searchValue);
    },
    removeFn(index) {
      this.selsetWrapShow = true;
      console.log("删除");
      this.inputData.splice(index, 1);
    },
  },
  mounted() {},
};
</script>

<style>
.selset_wrap {
  /* border: 1px solid; */
  margin-top: 5px;
  background: #ffffff;
  box-shadow: 3px 3px 3px 3px #999999;
}
.selset_wrap_title {
  color: #999999;
  height: 32px;
  width: 96%;
  line-height: 32px;
  margin: 0 auto;
  font-size: 12px;
}
.el-input-group__prepend {
  width: 60px;
}

.selset_wrap_list {
  /* height: 32px; */
  width: 100%;
  font-size: 12px;
  cursor: pointer;
}
.selset_wrap_list:hover {
  color: #fe774e;
  background: #f5f5f5;
}
.selset_wrap_list_list {
  width: 96%;
  line-height: 32px;
  margin: 0 auto;
  display: flex;
  flex-wrap: nowrap;
  justify-content: space-between;
}
.selset_wrap_list_list_name {
  width: 97%;
}
.selset_wrap_list_list_remove {
  width: 2%;
}
</style>```


  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
下面是一个基于ul和jquery的自定义下拉选择框的示例: HTML代码: ```html <div class="select-wrapper"> <input type="hidden" name="select" value=""> <div class="select-box">请选择</div> <ul class="select-options"> <li data-value="1">选项1</li> <li data-value="2">选项2</li> <li data-value="3">选项3</li> <li data-value="4">选项4</li> </ul> </div> ``` CSS代码: ```css .select-wrapper { position: relative; width: 200px; font-size: 14px; color: #333; } .select-box { position: relative; z-index: 1; padding: 10px; border: 1px solid #ccc; border-radius: 3px; cursor: pointer; } .select-options { position: absolute; top: 100%; left: 0; z-index: 2; display: none; margin: 0; padding: 0; list-style: none; background-color: #fff; border: 1px solid #ccc; border-top: none; border-radius: 0 0 3px 3px; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2); } .select-options li { padding: 10px; cursor: pointer; } .select-options li:hover { background-color: #f5f5f5; } ``` JS代码: ```javascript $(function() { $('.select-box').click(function() { $(this).siblings('.select-options').toggle(); }); $('.select-options li').click(function() { var value = $(this).data('value'); var text = $(this).text(); $(this).closest('.select-wrapper').find('input[type="hidden"]').val(value); $(this).closest('.select-wrapper').find('.select-box').text(text); $(this).closest('.select-options').hide(); }); $(document).click(function(event) { if (!$(event.target).closest('.select-wrapper').length) { $('.select-options').hide(); } }); }); ``` 该示例中,使用了一个包含隐藏输入框、下拉框按钮和下拉选项的`div`容器作为自定义下拉选择框的外层容器。下拉框按钮(`.select-box`)用于打开或关闭下拉选项(`.select-options`),下拉选项为一个`ul`列表,其中每个选项(`li`)都有一个`data-value`属性表示选项的值。 当用户点击某个选项时,使用jQuery将选项的值和文本分别设置到隐藏输入框和下拉框按钮中,并隐藏下拉选项。 当用户点击页面其它位置时,使用jQuery隐藏所有下拉选项。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值