Chr*_*isP 5 jquery jquery-ui jquery-ui-autocomplete
我有以下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);
}
});
Run Code Online (Sandbox Code Playgroud)
在显示自动完成列表之后,当按下向下箭头将焦点/突出显示移至项目#1时,item.value将显示在用户键入搜索条件的输入元素中。
从我的阅读中可以看出,在select事件中添加event.preventDefault(),如上面的代码所示,应该可以防止item.value插入到输入元素中。
但是,item.value仍在插入,并且选择事件中的断点不会被选中,直到选中突出显示的项目并按ENTER。
我希望原始搜索文本保留在输入元素中,因为突出显示在自动完成列表中的项目之间移动。
改用focus事件:
focus: function (event, ui) {
event.preventDefault();
}
Run Code Online (Sandbox Code Playgroud)