jQuery UI 自动完成 DownArrow UpArrow

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8045773/
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-08-27 09:55:47  来源:igfitidea点击:

jQuery UI Autocomplete DownArrow UpArrow

jqueryjquery-uiautocompletejquery-ui-autocompletearrow-keys

提问by Andy

I am having some issues with jQuery Autocomplete and moving DownArrow and UpArrow ?

我在使用 jQuery Autocomplete 和移动 DownArrow 和 UpArrow 时遇到了一些问题?

The problem seems to be that

问题似乎是

<input id="autocomplete-input" value="">

<input id="autocomplete-input" value="">

focus: function (event, ui) {
       $('#autocomplete-input').val(ui.item.label);
 }

This works great for MOUSE focus - but when I use arrowUpand arrowDown- it selects the ui.item.idover and above the ui.item.label

这对于鼠标焦点非常有用 - 但是当我使用arrowUparrowDown- 它选择ui.item.id超过ui.item.label

How can I fix this so that either:

我该如何解决这个问题,以便:

  1. the inputval isn't changed at all [i.e. it keeps the users inputted term]
  2. it updates the inputval with the focusedval the user is on with keydown/keyup
  1. inputVAL完全不改变[即它使用户输入项]
  2. input使用focused用户使用 keydown/keyup的val更新val

thanks

谢谢

回答by Andrew Whitaker

Make sure to prevent the default behavior of the focusevent:

确保阻止focus事件的默认行为:

focus: function (event, ui) {
    this.value = ui.item.label;
      // or $('#autocomplete-input').val(ui.item.label);

    // Prevent the default focus behavior.
    event.preventDefault();
      // or return false;
}