我正在使用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) 当使用Twitter Bootstrap(示例)中的预先输入功能时,当我按TAB并按Enter时,第一个建议被选中.但是,当用户不想使用其中一个建议时,只有一种方法可以实现这一点,即抓住鼠标并单击提交按钮.
我想要一种只使用键盘的方法,TAB或ARROW-UP看起来都很直观,但在这两种情况下我都无法获得Bootstrap的默认行为.我尝试了preventDefault()和stopPropagation().