我不知道如何使箭头键适用于我的自动建议框。我知道我需要在我的代码中添加这样的内容:
var keynum = 0;
if(window.event) { keynum = e.keyCode; } // IE
else if(e.which) { keynum = e.which; } // Netscape/Firefox/Opera
if(keynum === 38) { // up
//Move selection up
}
if(keynum === 40) { // down
//Move selection down
}
if(keynum === 13) { // enter
//Act on current selection
}
但我不知道如何将其与我的代码相匹配。有人可以帮我弄清楚这将如何与我的代码一起使用。当我按下向下箭头时,我希望突出显示的结果在按下向上箭头时向下移动一个并向上移动。这是我的代码:
</ul>
<div class="input-wrapper">
<input type="text" class="autosuggest" value="Type in a city or town" onblur="onBlur (this)" onfocus="onFocus(this)" > <input type="submit" value="Search">
<div class="dropdown">
<ul class="result"></ul>
</div>
</div>
$(document).ready(function() {
$('.autosuggest').keyup(function() {
var search_term = $(this).attr('value');
$.post('php/search.php', { search_term: search_term }, function(data) {
$('.result').html(data);
$('.result li').click(function(){
var result_value = $(this).text();
$('.autosuggest').attr('value', result_value);
$('.result').html('');
});
});
});
});
function onBlur(el) {
if (el.value == '') {
el.value = el.defaultValue;
}
}
function onFocus(el) {
if (el.value == el.defaultValue) {
el.value = '';
}
}