jQuery 自动完成 - 在选中(鼠标或输入)之前不填写文本输入

jQuery Autocomplete - don't fill text input until selected (mouse or enter)

我正在使用连接到数据库的 jQuery 自动完成功能。现在,如果您开始键入一个术语并将鼠标移到填充的列表上,则在您单击列表项之前,原始术语的文本输入不会改变。

我希望键盘也能发生同样的事情。键入向下箭头时不要填写文本框。使用 entermouse click

同时填充文本框和隐藏 ID
$(function() {
    function log( message ) {
        $(".userBookProjId").val(message);
        $(".userBookProjId").scrollTop( 0 );
    }
    $( ".userBookProj" ).autocomplete({
        source: "userBookProj.php?userId=" + $('#userId').val(),
        minLength: 2,//search after two characters
        select: function( event, ui ) {

            log( ui.item ? ui.item.id : "");
        }
    });
});

非常感谢任何帮助!

您将要使用 focus 事件。

$(function() {
  function log( message ) {
    $(".userBookProjId").val(message);
    $(".userBookProjId").scrollTop( 0 );
  }
  $( ".userBookProj" ).autocomplete({
    source: "userBookProj.php?userId=" + $('#userId').val(),
    minLength: 2,
    focus: function(){
      return false;
    },
    select: function( event, ui ) {
      log( ui.item ? ui.item.id : "");
    }
  });
});

focus( event, ui )

Type: autocompletefocus

Triggered when focus is moved to an item (not selecting). The default action is to replace the text field's value with the value of the focused item, though only if the event was triggered by a keyboard interaction.

Canceling this event prevents the value from being updated, but does not prevent the menu item from being focused.

查看更多:http://api.jqueryui.com/autocomplete/

希望对您有所帮助。