如何使用 Bootstrap typeaheads 插件 Typeahead.js 停止悬停以设置 typeahead 输入的值?

How to stop hover to set value of typeahead input using Bootstrap typeaheads plugin Typeahead.js?

我有一个使用 bootstrap typeahead plugin 的简单 ASP.NET MVC 应用程序。

我有一个如下所示的预输入:

这里的项目直到我点击才被选中。但是,假设我在没有点击的情况下悬停在列表中:

但是当我将鼠标移到表单外时,悬停的项目现在变为选中状态:

这意味着如果我点击网站上的任何地方,预先输入的 val() 将成为之前悬停的项目。

如何确保 val() 仅在我单击 typeaheadlist 中的项目时设置?

我已经设置了autoSelect: false设置,如果我还没有悬停(默认选择第一项)就解决了这个问题。

我为您提供的解决方案是编辑插件。一点都不难

  1. 从存储库下载插件 bootstrap3-typeahead.js
  2. 转到第 575 行并对该行进行注释。
blur: function (e) {
      if (!this.mousedover && !this.mouseddown && this.shown) {
        //this.select();
        this.hide();
        this.focused = false;
        this.keyPressed = false;
      } else if (this.mouseddown) {
        // This is for IE that blurs the input when user clicks on scroll.
        // We set the focus back on the input and prevent the lookup to occur again
        this.skipShowHintOnFocus = true;
        this.$element.focus();
        this.mouseddown = false;
      } 
},
  1. 替换代码:)

这应该行得通!对我有用 ;)