php+mysql+jquery實(shí)現(xiàn)簡(jiǎn)易的檢索自動(dòng)補(bǔ)全提示功能
本文實(shí)例講述了php+mysql+jquery實(shí)現(xiàn)簡(jiǎn)易的檢索自動(dòng)補(bǔ)全提示功能。分享給大家供大家參考,具體如下:
這段時(shí)間看了一些關(guān)于自動(dòng)補(bǔ)全提示方面的內(nèi)容,發(fā)現(xiàn)大部分實(shí)現(xiàn)過(guò)程都十分復(fù)雜。心想這應(yīng)該是一個(gè)比較簡(jiǎn)單的功能啊,于是自己親自動(dòng)手來(lái)小試一下。思路很常規(guī),需要說(shuō)明的是沒(méi)有為提示內(nèi)容綁定鍵盤(pán)事件,僅可用鼠標(biāo)操作。
html+jQuery內(nèi)容:
<html>
<head>
<style type="text/css">
#autoBox
{
margin: 0px;
padding: 0px;
border: 1px solid #CCCCCC;
width: 200px;
}
#autoBox li
{
clear: both;
background-color: white;
color: black;
position: relative;
top: 0px;
left: 0px;
line-height: 25px;
width:200px;
text-align: left;
overflow: hidden;
}
#autoBox li:hover
{
background-color: gray;
color: yellow;
cursor: pointer;
}
</style>
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(function(){
$.ajaxSetup({cache:false}) ; //不緩存
//以下代碼用于顯示檢索提示框
$("#autoBox").hide(); //初始化時(shí)隱藏補(bǔ)全提示框
$("#stuSearch").keyup(function(){ //輸入框中的keyup事件激活以下查詢行為
$("#autoBox").html(""); //先清空補(bǔ)全提示框原有內(nèi)容
if($("#stuSearch").val().length>0) // 如果輸入框不為空
{
$.ajax({ //后臺(tái)調(diào)用php文件進(jìn)行查詢
type:"post",
url:"phpFiles/stuSearch.php",
dataType:"json",
data:{keywords:$("#stuSearch").val()},
success:function(feedbackdata)
{
$("#autoBox").show();// 顯示補(bǔ)全提示框
for(i=0;i<feedbackdata.length;i++) //將結(jié)果添加到提示框中
{
$("#autoBox").append("<li>"+feedbackdata[i]['truename']+"</li>");
}
$("#autoBox li").on("click",function(){ //為這些新增的li綁定單擊事件,單擊后將其值賦到輸入框中
$("#stuSearch").val($(this).text());
})
$("#autoBox").append("<li style='text-align:right'>關(guān)閉</li>");//在提示框的最后添加一個(gè)li用來(lái)關(guān)閉
$("#autoBox li:last").on("click",function(){ // 添加單擊事件,單擊后隱藏提示框
$("#autoBox").hide();
})
}
});
}
})
})
</script>
</head>
<body>
<input type="text" placeholder="輸入您想檢索的關(guān)鍵詞后回車。" id="stuSearch" name="stuSearch"/>
<ul id="autoBox">
</ul>
</body>
</html>
stuSearch.php代碼
<?php
include '../phpFiles/connMysql.php';
$keywords=$_POST['keywords'];
$myrs=mysql_query("select * from users where truename like '".$keywords."%'");
if($myrs)
{
while($row=mysql_fetch_array($myrs))
{
$temp[]=$row;
}
echo(json_encode($temp));
}
?>
效果圖
輸入時(shí):

點(diǎn)擊某一項(xiàng)后:

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP+ajax技巧與應(yīng)用小結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語(yǔ)法入門(mén)教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
php實(shí)現(xiàn)統(tǒng)計(jì)郵件大小的方法
以下是對(duì)使用php實(shí)現(xiàn)統(tǒng)計(jì)郵件大小的方法進(jìn)行了分析介紹,需要的朋友可以過(guò)來(lái)參考下2013-08-08
php針對(duì)cookie操作的隊(duì)列操作類實(shí)例
這篇文章主要介紹了php針對(duì)cookie操作的隊(duì)列操作類,以實(shí)例形式分析了設(shè)置cookie的常見(jiàn)方法,以及隊(duì)列操作cookie的方法,最后給出了針對(duì)常見(jiàn)問(wèn)題的總結(jié),需要的朋友可以參考下2014-12-12
php中instanceof 與 is_a()區(qū)別分析
總的來(lái)說(shuō)php中instanceof 與 is_a()功能是一樣的,不同的是由于PHP的版本不同,部分PHP版本舍棄了is_a() 下面我們來(lái)具體分析下吧。2015-03-03
php在數(shù)據(jù)庫(kù)抽象層簡(jiǎn)單使用PDO的方法
這篇文章主要介紹了php在數(shù)據(jù)庫(kù)抽象層簡(jiǎn)單使用PDO的方法,以PDO針對(duì)數(shù)據(jù)庫(kù)的連接、插入、查詢等操作為例分析了PDO操作數(shù)據(jù)庫(kù)的相關(guān)技巧,需要的朋友可以參考下2015-11-11
部署PHP時(shí)的4個(gè)配置修改說(shuō)明
這篇文章主要介紹了部署PHP時(shí)的4個(gè)配置修改說(shuō)明,非常重要的四點(diǎn)配置修改,希望大家認(rèn)真閱讀本文。2015-10-10

