實現搜尋方塊自動提示功能(jquery+php)

來源:互聯網
上載者:User

標籤:

用戶端代碼

<html>
<head>
<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" class="k" /> <input type="button" name="s" value="搜尋" />
</div>
<div id="search_auto"></div>
</body>
</html>
<script src="jQuery.js"></script>
<script>
$(function(){
$(‘#search_auto‘).css({‘width‘:$(‘#search input[name="k"]‘).width()+4});
$(‘#search input[name="k"]‘).keyup(function(){
$.post(‘search_auto.php‘,{‘value‘:$(this).val()},function(data){
if(data==‘0‘) $(‘#search_auto‘).html(‘‘).css(‘display‘,‘none‘);
else $(‘#search_auto‘).html(data).css(‘display‘,‘block‘);
});
});
});
</script>

 

伺服器端代碼

<?php
$v=$_POST[value];
$re=mysql_query("select * from test where title like ‘%$v%‘ order by addtime desc limit 10");
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 class="cls"><a href="javascript:;" onclick="$(this).parent().parent().parent().fadeOut(100)">關閉</a& gt;</li>‘;
echo ‘</ul>‘;
?>

實現搜尋方塊自動提示功能(jquery+php)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.