樣本HTML響應:的jQuery返回false不停止滾動從服務器輸入後的keydown
<ul class="datalistPlaceholder hidden" style="display: block;">
<li><a class="tag" href="#">#<span style="font-weight: bold">ar</span>igato</a></li>
<li><a class="tag" href="#">#<span style="font-weight: bold">ar</span>izona</a></li>
<li><a class="tag" href="#">#cle<span style="font-weight: bold">ar</span>water</a></li>
</ul>
的jQuery:
$(document).ready(function(){
$('#search-field').keyup(function(e) {
ajaxAutocomplete.call(this, e);
});
});
function ajaxAutocomplete(e) {
var hash_tag = $.trim($(this).val());
$.ajax({
url : 'autocomplete.php',
method : 'GET',
dataType: 'html',
data : {tag : hash_tag}
})
.done(function(response) {
if (response) {
$('.datalistPlaceholder').html(response).show();
if (e.keyCode === 40) { // down key
$('.tag:first').focus();
$('.tag').keydown(function(e) {
down.call(this, e);
});
} else if (e.keyCode === 38) { // up key
}
} else {
$('.datalistPlaceholder').hide();
}
})
.fail(function() {
alert('Something went wrong');
});
}
function down(e)
{
if (e.keyCode === 40) {
$(this).parent('li').next().find('.tag').focus();
// stops page from scrolling
return false;
}
}
當用戶使用向下箭頭鍵選擇從下拉選項下拉菜單滾動頁面。我認爲回覆錯誤會阻止這一點。我究竟做錯了什麼?
而不是做'ajaxAutocomplete.call(這一點,E)的;'你爲什麼不只是這樣做:'$('#搜索 - 字段')。keyup(ajaxAutocomplete);'? –
因爲我需要知道按了什麼鍵 –