Home php教程 php手册 php+jquery 实现搜索提示功能实例

php+jquery 实现搜索提示功能实例

Jun 13, 2016 am 09:51 AM
google one Function Discover Example accomplish us hint search friend use Baidu of enter

用百度或google的朋友会发现只要我们输入一个字他们智能给我们提示相关的搜索引擎,这样我今天因为工作需要也要做一个这类的功能,下面我整理了两种关于jquery 实现用户输入搜索内容时自动提示实例,希望对大家有帮助。

今天突然想给本站做个搜索页面,这样用户可以通过搜索来找到自己喜欢的内容,也避免了在海量信息中手动查找资源的麻烦,我的目标和百度首页的效果类似,当用户输入要搜索的文字时,我们在下方给出相关的十条信息,如果用户要找的就是这十条信息内的某一条,那么简单,直接点击就可在新页面中打开页面,主要就是想更人性化一点,让用户使用起来更方便。

先看一下效果图吧,这样更有动力,要不然大家还不知道我在讲什么,到底要达到什么样的效果!

下面先主要讲解原理:

在search.html页面中,用户在搜索框内输入“j”时,使用javascript获取搜索框的文本内容,到数据库中查找相关的内容并返回,再使用javascript将服务器返回的结果显示在搜索框下面的提示框内,供用户参考选择。

具体的实现方法:

首先在页面中做好搜索框、搜索按钮、显示搜索提示的层,如下代码

 代码如下 复制代码


使用浏览器浏览页面,会看到下图的效果

 

看起来很普通,没什么样式,现在稍作样式上的调整

 代码如下 复制代码

#search{font-size:14px;}
#search .k{padding:2px 1px; width:320px;} /*将搜索框宽度设置大点*/

再将显示搜索提示的层样式调整一下,由于搜索提示层在搜索框正下方,所以我们设置其定位方式为绝对定位

 代码如下 复制代码

#search_auto{border:1px solid #817FB2; position:absolute;} /*设置边框、定位方式*/

接着用JS将搜索提示层的位置放置在搜索框正下方,且宽度和搜索框相同,这里我们采用jQuery来解决

 代码如下 复制代码

$(‘#search_auto’).css({‘width’:$(‘#search input[name="k"]‘).width()+4});

搜索提示层的位置和宽度已经确定好了,由于在用户没有输入搜索文字前这个提示框是不显示的,所以我们先要将它设置成隐藏,在提示层的样式里加上display:none将其隐藏。

已经全部OK了,现在只要给搜索框的onkeyup注册事件即可,我们依然采用jQuery来处理,在jQuery中是keyup

 代码如下 复制代码

$('#search input[name="k"]').keyup(function(){
$.post('search_auto.php',{'value':$(this).val()},function(data){  //向服务器上的search_auto.php发送post数据,$.post是jQuery的方法
if(data=='0') $('#search_auto').html('').css('display','none');  //判断服务器上返回的数据,如果等于0,则表示没有找到相关的内容,所以将提示框的内容清空并隐藏
else $('#search_auto').html(data).css('display','block');  //如果服务器上返回的数据不等于0,则将返回的内容放到提示框内并显示提示框
});
});

上面客户端已经做好了,已经可以将用户输入的内容发送到服务器端,并响应服务器的返回值。

那么服务器端如何处理客户端发送来的数据呢,下面用PHP来举个例子

 代码如下 复制代码

$v=$_POST[value];
$re=mysql_query("select * from test where title like '%$v%' order by addtime desc limit 10");  //根据客户端发送来的数据,到数据库中查询10条相关的结果
if(mysql_num_rows($re) echo '

    ';
    while($ro=mysql_fetch_array($re)) echo '
  • '.$ro[title].'
  • ';  //将查询得到的相关结果的标题输出,这个地方需要注意,由于通过jQuery的ajax技术返回的文本是UTF-8编码,所以如果$ro[title] 中包含中文,一定要记得用PHP的iconv或其它函数将其转换成UTF-8编码,否则在页面中看到的会是一串乱码
    echo '
  • 关闭& gt;
  • ';  //输入一个关闭按钮,让用户不想看到提示层时可以点击关闭,关闭按钮采用jQuery,解释一下,当前点击的按钮是$(this),一直向上找到其第三个父元素,让它逐渐消失
    echo '
';
?>

现在服务器已经可以正确的执行我们发送过去的数据了,并且返回相应的结果,那么现在在搜索框内输入一个文字测试一下吧,但前提是你的数据库中得有与这个文字相关的内容啊,要不然你也看不到提示框的出现,因为没有相关提示内容啊,呵呵。

可是还有点美中不足,那就是提示框里面的内容不美观,和我们在百度搜索中看到的提示框相比,简直是太丑了,哈哈,不急,我们再用css来调整显示的效果

 代码如下 复制代码

#search_auto li{background:#FFF; text-align:left;} /*设置提示框内的li标签效果*/
#search_auto li.cls{text-align:right;} /*设置提示框内的关闭按钮效果*/
#search_auto li a{display:block; padding:5px 6px; cursor:pointer; color:#666;} /*设置提示框内li标签下的a标签效果*/
#search_auto li a:hover{background:#D8D8D8; text-decoration:none; color:#000;} /*当鼠标移入提示框内时的效果*/

现在才算是真正的完全制作完成,至于要不要设置一个延迟处理,或是其它更完善的功能,留给朋友们自己开动脑筋了,大家也可以在下面回复你的想法,等等。

客户端完整代码:

 代码如下 复制代码




jquery+php实现用户输入搜索内容时自动提示






<script><br /> $(function(){</script>

$('#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');
});
});

});

 

服务器端完整代码:

 代码如下 复制代码

$v=$_POST[value];
$re=mysql_query("select * from test where title like '%$v%' order by addtime desc limit 10");
if(mysql_num_rows($re) echo '

';
?>


方法二,使用jquery.input_complete插件了,这个非常简单

 代码如下 复制代码


html只要简单的

 代码如下 复制代码

tt" class="sel_quy" autocomplete="off" />

注意这里在里的ID必须TT哦,这个你可以自己定义名字,同时jquery.input_complete插件与jquery大家自己去下载吧。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Google Pixel 9 Pro XL gets tested with desktop mode Google Pixel 9 Pro XL gets tested with desktop mode Aug 29, 2024 pm 01:09 PM

Google has introduced DisplayPort Alternate Mode with the Pixel 8 series, and it's present on the newly launched Pixel 9 lineup. While it's mainly there to let you mirror the smartphone display with a connected screen, you can also use it for desktop

Google Tensor G4 of Pixel 9 Pro XL lags behind Tensor G2 in Genshin Impact Google Tensor G4 of Pixel 9 Pro XL lags behind Tensor G2 in Genshin Impact Aug 24, 2024 am 06:43 AM

Google recently responded to the performance concerns about the Tensor G4 of the Pixel 9 line. The company said that the SoC wasn't designed to beat benchmarks. Instead, the team focused on making it perform well in the areas where Google wants the c

deepseek web version entrance deepseek official website entrance deepseek web version entrance deepseek official website entrance Feb 19, 2025 pm 04:54 PM

DeepSeek is a powerful intelligent search and analysis tool that provides two access methods: web version and official website. The web version is convenient and efficient, and can be used without installation; the official website provides comprehensive product information, download resources and support services. Whether individuals or corporate users, they can easily obtain and analyze massive data through DeepSeek to improve work efficiency, assist decision-making and promote innovation.

Google opens AI Test Kitchen & Imagen 3 to most users Google opens AI Test Kitchen & Imagen 3 to most users Sep 12, 2024 pm 12:17 PM

Google's AI Test Kitchen, which includes a suite of AI design tools for users to play with, has now opened up to users in well over 100 countries worldwide. This move marks the first time that many around the world will be able to use Imagen 3, Googl

Pixel 9 Pro XL vs iPhone 15 Pro Max camera comparison reveals surprising Google wins in video and zoom performance Pixel 9 Pro XL vs iPhone 15 Pro Max camera comparison reveals surprising Google wins in video and zoom performance Aug 24, 2024 pm 12:32 PM

The Google Pixel 9 Pro and Pro XL are Google's answers to the likes of the Samsung Galaxy S24 Ultra and the Apple iPhone 15 Pro and Pro Max. Daniel Sin on YouTube(watch below) has compared the Google Pixel 9 Pro XL to the iPhone 15 Pro Max with some

Google confirms Pixel 9 Pro Fold incompatibility with official wireless charging accessories thanks to curious design Google confirms Pixel 9 Pro Fold incompatibility with official wireless charging accessories thanks to curious design Sep 01, 2024 am 06:31 AM

The Pixel 9 Pro Fold is akin to a volte-face for Google's nascent foldable division. In fact, such are the differences between thePixel 9 Pro Foldand the Pixel Fold that the former has been compared more against the OnePlus Open (curr. $1,399.99 on A

First Google Pixel 9a footage shows full redesign with much earlier release also rumoured First Google Pixel 9a footage shows full redesign with much earlier release also rumoured Aug 27, 2024 am 06:35 AM

Hardly any time has passed since Google announced its first Pixel 9 series smartphones. In fact, it has only just started shipping the Pixel 9, Pixel 9 Pro and Pixel 9 Pro XL (curr. $1,099 on Amazon). By contrast, Pixel 9 Pro Fold pre-orders will not

ai tool recommendation ai tool recommendation Nov 29, 2024 am 11:08 AM

This article introduces six popular AI tools, including Douyin Doubao, Wenxin Yige, Tencent Zhiying, Baidu Feipiao EasyDL, Baidu AI Studio and iFlytek Spark Cognitive Large Model. These tools cover different functions such as text creation, image generation, video editing, and AI model development. Choosing the right AI tool requires consideration of factors such as functional requirements, technical level, and cost budget. These tools provide convenient and efficient solutions for individuals and businesses in need of AI assistance.

See all articles