我只想为我的自动建议框提供 5 个结果,但不知道该怎么做。任何帮助将不胜感激这是我的代码:
<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 = '';
}
}