javascript - auto-suggest keyboard event not working on keyup event -
i creating auto-suggest scratch. using jquery-ajax function data server. doing filtering data on keyup event auto suggestion.
now after filtering want navigate list items using keyboard's arrow keys problem every time pressing arrow key list being refreshed on keyup event clears selection happened through arrow key.
i track trace code , came know reason. here 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: // 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 suggest me solution of problem?
try this,
$("#inputfield").keyup(function(){ if(keycode!=38 || keycode!=40){ //proceed fetch suggestion... }else{ //proceed navigation.. } });
Comments
Post a Comment