javascript 自动完成 jquery ui 中的按键事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15137752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:38:54  来源:igfitidea点击:

keypress event in autocomplete jquery ui

javascriptjqueryjquery-ui-autocomplete

提问by Ashwin

I want to show the value of the autocomplete suggesion on textbox only when the user is selecting the suggession. I tried

我只想在用户选择建议时在文本框上显示自动完成建议的值。我试过

$("#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();
    }
});

but the value is cleared in textbox when I hover the mouse over the suggestion but not when I press up and down arrow keys.

但是当我将鼠标悬停在建议上时,文本框中的值会被清除,但当我按下向上和向下箭头键时则不会。

回答by sasi

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

try this..instead of your keypress event

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