我刚刚在我的页面中添加了一个搜索栏。几乎一切正常,但是,我希望在字段外单击后自动建议消失。我该怎么做?我正在使用 HTML5,我的代码是:
$(document) .ready(function() {
$('.autosuggest').keyup(function() {
var search_term = $(this).val();
$.post('search.php', {search_term:search_term}, function(data) {
$(".result").html(data);
$('.result li').click(function() {
var result_value = $(this).text();
$('.autosuggest').val(result_value);
$('.result').html('');
});
});
});
});
<input type="text" id="autosuggest" class="autosuggest" onBlur="removeSuggestion();"> <input type="submit" value="search">
<div class="dropdown">
<ul class="result"></ul>
</div>
不太确定我的php是否需要,但以防万一......
<?php
require_once 'connect.php';
if (isset($_POST['search_term']) == true && empty($_POST['search_term']) == false) {
$search_term = mysql_real_escape_string($_POST['search_term']);
$query = mysql_query("SELECT town FROM `boroughs` WHERE town LIKE '$search_term%'");
while (($row = mysql_fetch_assoc($query)) !== false) {
echo '<li>', $row['town'], '</li>';
}
}
?>