我正在使用bootstrap typeahead,我想知道,当单击enter按钮时,如何让它提交?换句话说,如果我在typeahead输入中输入"hello“并按enter,我将如何提交"hello"?
我已经这样做了,所以typeahead不会自动选择第一个结果。
这是我的代码:
<form method="POST" action="script.php">
<input name="typeahead_field" class="typeahead" data-provide="typeahead" autocomplete="off" type="text">
</form>jQuery:
$('.typeahead').typeahead({
items: 10,
source: function (query, process) {
$.ajax({
url: 'typeahead.php',
type: 'POST',
dataType: 'JSON',
data: 'query=' + query,
success: function(data) {
process(data);
}
});
},
highlighter: function(data) {
// code that returns each result
return html;
},
updater: function(data) {
// code that redirects the user if they click the selection
},
});请帮帮我!
发布于 2016-11-14 08:45:49
这是个老问题,但值得回答。以下是一种可能的解决方案:
$('.typeahead').on('keydown', function(e) {
if (e.keyCode == 13) {
var ta = $(this).data('typeahead');
var val = ta.$menu.find('.active').data('value');
if (!val)
$('#your-form').submit();
}
}编辑:第一次尝试非常幼稚:)
发布于 2014-01-21 08:18:02
您需要一个submit按钮来启用submit on enter。您可以使用此CSS隐藏该按钮:
<input type="submit" style="position: absolute; left: -9999px">发布于 2021-07-06 03:04:00
效果很好,但不像鼠标悬停事件那样突出显示所选内容。也许这个解决方案会更好地工作,因为它似乎封装了前面答案的要点。https://bwbecker.github.io/blog/2015/08/29/pain-with-twitter-typeahead-widget/
https://stackoverflow.com/questions/21246322
复制相似问题