Bootstrap Typeahead - 不自动选择第一项?

Pez*_*lio 10 javascript typeahead coffeescript twitter-bootstrap

我正在使用Twitter Bootstrap typeahead库的这个分支,它允许异步数据源以及onselect事件.到目前为止,它对我来说非常好用,但是当用户选中该字段时(即没有主动选择下拉条目),会激活onselect事件(在我的情况下,将用户重定向到另一个页面).如果用户没有点击,有什么方法可以阻止onselect事件被触发?这是我到目前为止所得到的(在CoffeeScript中):

$(document).ready ->
  $('#inspection_name').typeahead(
    source: (typeahead, query) ->
      $.ajax(
        url: "/inspections/search.json?name="+query
        success: (data) =>
          return_list = []
          $(data.results.inspections).each ->
            return_list.push("<span data-url='" + this.uri + "/edit'>" + this.name + ", " + this.town + "</span>") 

          typeahead.process(return_list)
      )

    onselect: (obj) =>
      window.location.href = $(obj).attr("data-url")
  )
Run Code Online (Sandbox Code Playgroud)

Pet*_*use 6

对于默认的Bootstrap,这将起作用:

$.fn.typeahead.Constructor.prototype.render = function (items) {

var that = this;

items = $(items).map(function (i, item) {
  i = $(that.options.item).attr('data-value', item);
  i.find('a').html(that.highlighter(item));
  return i[0];
});

this.$menu.html(items);
return this;
};
Run Code Online (Sandbox Code Playgroud)

在包括bootstrap之后的某个地方.(分钟)js

  • 我正在使用标准的,默认的Bootstrap,但这也有效,除了它还引入了一个错误,当我点击"ENTER"键时,它不再执行搜索(我必须手动点击我有的提交按钮). (2认同)

Pez*_*lio 5

最后,我回答了我自己的问题,并将调整放在这个要点中:

https://gist.github.com/1977953