vue 移动端键盘搜索事件监听

1、首先注意,input的type="serch"

  <input @keypress="searchGoods" type="serch" placeholder="搜索商品">

2、监听keypress事件

(1)KeyDown、KeyUp 事件

  • 这些事件是当一个对象具有焦点时按下 ( KeyDown ) 或松开 ( KeyUp ) 一个键时发生的。(要解释 ANSI 字符,应使用 KeyPress 事件。)

(2)KeyPress 事件

  • 此事件当用户按下和松开一个 ANSI 键时发生。

3、阻止事件默认行为

  •   methods中添加 searchGoods方法, 
  • 判断keyCode ==13  
  •  阻止默认事件(默认是换行)
  • 通过event.target.value获取要搜索的值,调用搜索接口。
  searchGoods(event) { 
      if (event.keyCode == 13) { //如果按的是enter键 13是enter 
          event.preventDefault(); //禁止默认事件(默认是换行) 
          console.log(event.target.value)
          Toast("点击了确认") 
      } 
      } 

猜你喜欢

转载自blog.csdn.net/Dong8508/article/details/81780491