自动完成jquery ui中的keypress事件

Ash*_*win 7 javascript jquery jquery-ui-autocomplete

我想仅在用户选择suggession时才在文本框中显示自动完成建议的值.我试过了

$("#trainerNameAutoComplete").autocomplete({
    source:"serverpage.php?id="+1,
    minLength:1,
    focus: function( event, ui ){
        $("#trainerNameAutoComplete").val('');
    },
    keypress: function(event,ui){
        if ((event.which == 38||event.Keycode ==38) || (event.which == 40||event.Keycode ==40)) {
            console.log("key down");
            $("#trainerNameAutoComplete").val('');
        }
    },
    select:function(event,ui){
        somefunction();
    }
});
Run Code Online (Sandbox Code Playgroud)

但是当我将鼠标悬停在建议上时,该值在文本框中被清除,但是当我按下向上和向下箭头键时则没有.

sas*_*asi 1

 keydown: function(event,ui){
        event.preventDefault();
        if (event.Keycode ==38||event.Keycode ==40) {
            console.log("key down");
            $("#trainerNameAutoComplete").val('');
        }
    },
Run Code Online (Sandbox Code Playgroud)

试试这个..而不是你的按键事件