当我有一个来自自动完成的建议列表时,我需要允许用户按 Enter 键来处理他们输入的搜索词或单击其中一个建议。现在按回车键似乎提交表单(没有操作)。
<script type="text/javascript" language="JavaScript">
jQuery( document ).ready(function($) {
$('#SearchPhrase').autocomplete({
source: function(query, response) {
$.ajax({
url: "/services/search.cfc?method=getMessages&returnformat=json",
dataType: "json",
data: {
term: query.term
},
success: function(result) {
response(result);
}
});
}
,
minLength: 3,
select: function(event, ui) {
alert(ui.item.value);
$.ajax({
url: "/services/search.cfc?method=getMessages2&returnformat=json",
dataType: "json",
data: {
term: ui.item.value
},
success: function(result) {
alert(result.id);
}
});
}
}).keydown(function(e){
if (e.keyCode === 13){
alert('enter was pressed');
event.preventDefault();
$.ajax({
url: "/services/search.cfc?method=getMessages2&returnformat=json",
dataType: "json",
data: {
term: $('#SearchPhrase').val()
},
success: function(result) {
alert(result.id);
}
});
}
});
});
</script>