【发布时间】:2016-12-01 22:00:35
【问题描述】:
我正在使用this fork of the Twitter Bootstrap typeahead library,它允许异步数据源以及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")
)
【问题讨论】:
-
我现在实际上已经回答了我自己的问题。我对原始 Gist 进行了小幅编辑,以停止选项卡时的默认行为。 You can see my adjustments, together with a Diff here
-
谢谢,Gist 真的很有用。如果您将评论变成答案,我会投票赞成。
标签: javascript coffeescript twitter-bootstrap typeahead