【发布时间】:2017-12-31 00:42:55
【问题描述】:
我有以下代码:
var students = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('fullName'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/api/students?query=%QUERY',
wildcard: '%QUERY'
}
});
$('#student').typeahead({
minLength: 2,
highlight: true,
},
{
name: 'students',
display: function (item) { return item.fullName },
source: students.ttAdapter(),
templates: {
empty: [
'<div class="tt-empty-message">' +
'No results found' +
'</div>'
]
}
}).on("typeahead:select",
function (e, student) {
console.log("inside isselect");
isSelected = true;
vm.studentId = student.id;
});
我试图捕捉一个场景,如果用户输入一些乱码或除预先输入的值之外的其他内容并单击提交按钮,那么我需要显示一条错误消息。为此,我需要捕获 typeahead 列表并查看 typeeahed 列表是否包含输入值。我怎样才能做到这一点?
我已经搜索这个解决方案 5 个小时了。我已经在其他线程上查看并实现了类似的答案,但似乎没有任何效果。例如看看这个答案:
http://jsfiddle.net/Fresh/bbzt9hcr/
我尝试了 baove 代码,但 typeahed 甚至没有出现。而我的代码工作得很好。请让我知道如何解决这个问题。先感谢您。
【问题讨论】:
-
将整个对象存储在选择事件中...然后在提交表单时进行比较
-
我不认为这会起作用,因为选择甚至只包含一个对象,而我需要捕获预先输入的列表并检查当用户点击提交按钮时列表是否包含用户输入.因此,每次用户进行预先输入选择时,列表都会更新。但我不知道如何捕获列表。
-
他们一次只选择一个项目......与最后选择的项目相比,并存储在变量中
-
呃!非常感谢。那行得通。你能帮我解决这个问题吗:stackoverflow.com/questions/48036930/…
标签: javascript jquery typeahead.js typeahead