vue input输入框实时搜索

本文介绍如何在 Vue 中创建一个输入框,实现实时搜索功能。通过示例代码展示具体的实现过程。

废话不多说先看效果:
请添加图片描述
代码如下:

<template>
  <div style="padding-left: 3px; margin-top: 3px">
    <!-- 搜索框 -->
    <div class="input-warp">
      <input
        class="input"
        v-model="keyWord"
        type="text"
        placeholder="输入城市名称进行查找"
      />
      <el-button class="input-search" icon="el-icon-search" circle></el-button>
    </div>
    <div class="searchList" v-if="keyWord">
      <ul>
        <li
          v-for="(item, index) in searchData"
          :key="index"
          @click="chooseCode(item)"
        >
          {{ item.name }}
        </li>
        <li v-if="searchData.length == 0">暂无相关城市,请换个城市进行搜索</li>
      </ul>
    </div>
  </div>
</template>

<script>
import { searchCityList } from "@/api/homeApi";
export default {
  data() {
    return {
      keyWord: "",
      searchData: [],
    };
  },

  created() {},
  watch: {
  //这里监听监听输入框的值,如果发生改变执行接口方法
    keyWord: {
      handler(newv, oldv) {
        this.getSearchData();
      },
    },
  },
  mounted() {},
  methods: {
  //获取用户输入城市的列表
    getSearchData() {
      searchCityList({ cityName: this.keyWord }).then((res) => {
        if (res.success == true) {
          console.log(res.data);
          this.searchData = res.data.list;
        }
      });
    },
    //当用户点击搜索出来的城市之后
    chooseCode(data) {
      // console.log(data);
      this.$store.commit("CHANGE_CITYCODE", data.code);
      this.$store.commit("CHANGE_CITYNAME", data);
      this.$store.commit("CHANGE_CITYDIALOG", false);

      if (window.location.pathname == "/houseDetail") {
        this.$router.push({ path: "/house" });
      } 
      this.$router.go(0);
    },
  },
};
</script>

<style scoped lang="less">
// 搜索框
.input-warp {
  position: relative;
  .input {
    border: none;
    width: 766px;
    height: 50px;
    box-sizing: border-box;
    opacity: 0.9;
    background: #ffffff;
    border: 1px solid #fff;
    padding: 0 55px 0 25px;
    border-radius: 25px;
    box-shadow: 0px 1px 4px 0px rgba(146, 146, 146, 0.6);
    &:focus-visible {
      outline: none;
    }
  }
  ::v-deep .input-search {
    position: absolute;
    top: 7px;
    right: 422px;
    color: #fff;
    text-align: center;
    width: 36px;
    height: 36px;
    opacity: 1;
    background: linear-gradient(90deg, #3493ff, #485cff);
    border-radius: 50%;
    box-shadow: 0px 1px 4px 0px rgba(146, 146, 146, 0.6);
    .el-icon-search {
      font-size: 19px;
      margin: -4px;
    }
  }
}
.searchList {
  width: 766px;
  // height: 300px;
  box-shadow: 0px 1px 4px 0px rgb(146 146 146);
  padding: 15px 0;
  // padding-left: 26px;
  box-sizing: border-box;
  overflow: auto;
}
li {
  list-style: none;
  line-height: 40px;
  font-size: 16px;
  padding-left: 26px;
}
li:hover {
  cursor: pointer;
  background: #f8f8f9;
}
</style>

### 实现 VueInput 输入框按下回车键自动提交表单 在 Vue.js 应用程序中,可以通过监听键盘事件来处理 `input` 或者 `textarea` 组件中的 Enter 键按下行为。为了实现当用户在一个输入字段内按下回车键时触发特定逻辑或提交表单的功能,可以利用 v-on 指令绑定 keydown 事件并检查 keyCode 是否等于 13(代表 Enter)。下面是一个简单的例子说明如何做到这一点: ```html <template> <div id="app"> <!-- 使用 @keyup.enter 替代传统的 keypress 方式 --> <input type="text" placeholder="Press enter to submit..." v-model="message" @keyup.enter="handleSubmit"/> </div> </template> <script> export default { data() { return { message: '' } }, methods: { handleSubmit(event) { console.log('Form submitted with value:', this.message); // 这里放置实际的提交逻辑 event.target.blur(); // 提交后使输入失去焦点可选操作 } } } </script> ``` 上述代码展示了通过 `@keyup.enter` 直接简化了对于按键事件类型的判断过程[^1]。 另外一种方式则是更细致地控制,即手动捕获所有的按键事件并通过 JavaScript 来检测具体的按键码是否为 13 (Enter),这种方式适用于需要额外条件判断的情况: ```javascript // 手动捕捉所有按键事件并仅针对 Enter 响应 <input type="text" @keydown="handleKeydown"> methods: { handleKeydown(e){ if(e.key === 'Enter'){ e.preventDefault(); alert(`You pressed the "Enter" key! Value is ${this.message}`); } } } ``` 这两种方法都可以有效地实现在 Vue 环境下的输入框按回车键触发表单提交或其他自定义功能的需求[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值