我在文本框中使用 bootstrap typeahead 通过 Ajax 调用绑定(bind)用户列表 这是我的代码
$("#userMapping_User").typeahead({
source: function (query, process) {
return $.get($_GetUsers_ByLoggedInUser, { term: query }, function (result) {
var resultList = [];
$.map(result, function (item, id) {
if (item && (item.first_name || item.last_name)) {
var displayName = item.first_name + ' ' + item.last_name;
var user = { id: item.UserId, name: displayName ? displayName.trim() + ' - ' + item.email : '' };
resultList.push(user);
}
});
return process(resultList);
});
},
minLength: 2,
autoSelect: true,
afterSelect: function (e) {
$("#userMapping_SelectedUserId").val(e.id);
$("#userMapping_User").removeClass('form-error');
$(".error-block").remove();
}
});
但问题是我想禁用自由文本输入,以便用户无法输入无效值,只能插入列表中可用的值?
最佳答案
是的,您可以使用以下代码来完成此操作。
var myData = [...]; // This array will contain all your possible data for the typeahead
$("#my-type-ahead").typeahead({source : myData})
.blur(validateSelection);
function validateSelection() {
if(source.indexOf($(this).val()) === -1)
alert('Error : element not in list!');
}
这是 @samuel 在 Link 中给出的答案。但我想你可以通过使用回调函数并防止输入值来做得更好。这是你的选择。
干杯..!!
关于javascript - typeahead 不允许自由文本输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51719828/