简体   繁体   English

自动建议键盘事件不适用于keyup事件

[英]auto-suggest keyboard event not working on keyup event

I am creating auto-suggest from scratch. 我正在从头开始创建自动建议。 I am using jquery-ajax function to get the data from server. 我正在使用jquery-ajax函数从服务器获取数据。 I am doing filtering data on keyup event for auto suggestion. 我正在过滤keyup事件上的数据以进行自动建议。

Now after filtering I want to navigate the list items using keyboard's arrow keys but the problem is every time I am pressing the arrow key the list is being refreshed on keyup event which clears the selection happened through arrow key. 现在,在过滤之后,我想使用键盘的箭头键浏览列表项,但是问题是每次我按箭头键时,都会在keyup事件中刷新列表,该事件清除了通过箭头键进行的选择。

I track trace my code and I came to know this reason. 我跟踪跟踪我的代码,我才知道这个原因。 Here is my code : 这是我的代码:

var $listItems = $('li.suggestion-item');
   var key = event.keyCode,$current,
   $selected = $listItems.filter('.selected');
   if ( key != 40 && key != 38 ) { return key; }
   $listItems.removeClass('selected');
   switch(key){    
     case 40: // Down key
       if (! $selected.length || $selected.is(':last-child')) {
            $current = $listItems.eq(0).addClass('selected');
       }
       else {
            $current = $selected.removeClass('selected').next().addClass('selected');
       }
           break;
       case 38: // Up key
       if ( $selected.length ===1 || $selected.is(':first-child') ) {
            $current = $listItems.last();
       }
       else {
          $current = $selected.prev();
       }
       break;
       case 13:
            $('.suggestion-item, .error').hide();
            break;      
    }
    } else {
          $('#validline').removeClass('true');
          $('.suggestion-item, .error').hide();      
     }
 }

can anybody suggest me the solution of this problem? 有人可以建议我解决这个问题吗?

Try like this, 这样尝试

$("#inputField").keyup(function(){
    if(keycode!=38 || keycode!=40){
        //proceed to fetch suggestion...
    }else{
        //proceed navigation..
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM