index.html
<html>
<head>
<Meta charset="utf-8">
<style>
#search{font-size:14px;}
#search .k{padding:2px 1px; width:320px;}
#search_auto{border:1px solid #817FB2; position:absolute; display:none;}
#search_auto li{background:#FFF; text-align:left;}
#search_auto li.cls{text-align:right;}
#search_auto li a{display:block; padding:5px 6px; cursor:pointer; color:#666;}
#search_auto li a:hover{background:#D8D8D8; text-decoration:none; color:#000;}
</style>
<title>jquery+PHP实现用户输入搜索内容时自动提示</title>
</head>
<body>
<div id="search">
<input type="text" name="k" /> <input type="button" name="s" value="搜索" />
</div>
<div id="search_auto"></div>
<script src="jquery.min.js"></script>
<script>
$(function(){
$('#search_auto').css({'width':$('#search input[name="k"]').width()+4});
$('#search input[name="k"]').keyup(function(){
value = $(this).val();
$.post('search_auto.PHP',{'value':$(this).val()},function(data){
console.log(data);
if(data=='0') $('#search_auto').html("").css('display','none');
else $('#search_auto').html(data).css('display','block');
});
});
});
</script>
</body>
</html>
=====================================================
search_auto.PHP
<?PHP
$v=$_POST['value'];
$conn=MysqL_connect("servername ","username","password");
$re=MysqL_query("dataname","select * from `db_table` where title like '%$v%' order by addtime desc limit 10",$conn);
if(MysqL_num_rows($re)<=0) exit('0');
echo '<ul>';
while($ro=MysqL_fetch_array($re)) echo '<li><a href="">'.$ro['title'].'</a></li>';
echo '<li><a href="javascript:;" onclick="$(this).parent().parent().parent().fadeOut(100)">关闭</a& amp; gt;</li>';
echo '</ul>';
?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。