【发布时间】:2019-01-27 05:04:31
【问题描述】:
我正在使用 typeahead.js 0.9.3,是否可能 typehead 只允许列表中的项目而不是用户手动输入的文本。它不允许用户这样做或在任何事件之后检查该值是否存在于列表中。
$(document).ready(function(){
$('input.typeahead').typeahead({
name: 'typeahead',
remote:'sale/files/search_part.php?key=%QUERY',
limit : 10
});
});
我不擅长 javascript 和 JQuery,所以不知道如何将数组值设置为列表,所以可以在下面的代码中检查它是否存在于列表中。请帮忙
var myData = [...]; // how to assign output list to this array?
$("#partno").typeahead({source : myData}).blur(validateSelection);
function validateSelection() {
if(source.indexOf($(this).val()) === -1) {
alert('Error : element not in list!');
}
}
输入框
<input type="text" name="typeahead" class="typeahead tt-query" autocomplete="off" spellcheck="false" id="partno" >
【问题讨论】:
-
您使用的是哪个 typeahead 插件?有几个可用的
-
typeahead.js 0.9.3
-
您可以使用
notFound/empty自定义模板来显示您的消息。 github.com/twitter/typeahead.js/blob/master/doc/… -
感谢您的宝贵时间,但在我的情况下,它们都不起作用,因为“notfound”用于请求查询,“empty”用于查询目的。
标签: javascript jquery typehead