我有以下 jQueryUI 自动完成功能
$('#clientSearch').autocomplete({
source: function (request, response) {
var url = window.apiUrl + '/clients?searchText=' + request.term;
var request = $.ajax({
url: url,
type: 'GET',
dataType: "json",
cache: false,
contentType: 'application/json; charset=utf-8'
});
request.done(function (clientList) {
response($.map(clientList, function (client) {
return {
label: client.lastName + ', ' + client.firstInitial + '. ' + client.sex + '/' + client.age,
value: client.id
}
}));
});
request.fail(function (jqXHR, textStatus, errorThrown) {
response(
{
label: 'Error retrieving clients',
value: null
}
);
});
},
minLength: 2,
select: function (event, ui) {
event.preventDefault();
getClient(ui.item.value);
}
});
显示自动完成列表后,当按下向下箭头将焦点/突出显示到项目 #1 时,item.value 将显示在用户键入搜索条件的输入元素中。
根据我所读到的内容,在 select 事件中添加 event.preventDefault() (如上面的代码中所做的那样)应该会阻止 item.value 被插入到输入元素中。
但是,item.value 仍在被插入,并且在使用 ENTER“选择”突出显示的项目之前,不会命中 select 事件中的断点。
我希望原始搜索文本保留在输入元素中,因为突出显示在自动完成列表中的项目之间移动。