2013-02-28 72 views
7

我只想在用户选择suggession时在文本框中显示自动填充建议的值。我试过自动完成jquery UI中的按键事件

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

但是当我将鼠标悬停在建议上而不是当我按上下方向键时,该值在文本框中被清除。

回答

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

尝试this..instead你的按键事件

+0

它不工作 – Ashwin 2013-02-28 14:53:40

+0

我刚刚更新现在检查.. – sasi 2013-03-01 03:43:31

+1

我已经检查了它的不是现在的工作 – Ashwin 2013-03-01 06:55:27