php+jquery 实现搜索提示功能实例
用百度或google的朋友会发现只要我们输入一个字他们智能给我们提示相关的搜索引擎,这样我今天因为工作需要也要做一个这类的功能,下面我整理了两种关于jquery 实现用户输入搜索内容时自动提示实例,希望对大家有帮助。
今天突然想给本站做个搜索页面,这样用户可以通过搜索来找到自己喜欢的内容,也避免了在海量信息中手动查找资源的麻烦,我的目标和百度首页的效果类似,当用户输入要搜索的文字时,我们在下方给出相关的十条信息,如果用户要找的就是这十条信息内的某一条,那么简单,直接点击就可在新页面中打开页面,主要就是想更人性化一点,让用户使用起来更方便。
先看一下效果图吧,这样更有动力,要不然大家还不知道我在讲什么,到底要达到什么样的效果!
下面先主要讲解原理:
在search.html页面中,用户在搜索框内输入“j”时,使用javascript获取搜索框的文本内容,到数据库中查找相关的内容并返回,再使用javascript将服务器返回的结果显示在搜索框下面的提示框内,供用户参考选择。
具体的实现方法:
首先在页面中做好搜索框、搜索按钮、显示搜索提示的层,如下代码
代码如下 | 复制代码 |
|
使用浏览器浏览页面,会看到下图的效果
看起来很普通,没什么样式,现在稍作样式上的调整
代码如下 | 复制代码 |
#search{font-size:14px;} |
再将显示搜索提示的层样式调整一下,由于搜索提示层在搜索框正下方,所以我们设置其定位方式为绝对定位
代码如下 | 复制代码 |
#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(){ |
上面客户端已经做好了,已经可以将用户输入的内容发送到服务器端,并响应服务器的返回值。
那么服务器端如何处理客户端发送来的数据呢,下面用PHP来举个例子
代码如下 | 复制代码 |
$v=$_POST[value];
while($ro=mysql_fetch_array($re)) echo ' echo ' echo ' ?> |
现在服务器已经可以正确的执行我们发送过去的数据了,并且返回相应的结果,那么现在在搜索框内输入一个文字测试一下吧,但前提是你的数据库中得有与这个文字相关的内容啊,要不然你也看不到提示框的出现,因为没有相关提示内容啊,呵呵。
可是还有点美中不足,那就是提示框里面的内容不美观,和我们在百度搜索中看到的提示框相比,简直是太丑了,哈哈,不急,我们再用css来调整显示的效果
代码如下 | 复制代码 |
#search_auto li{background:#FFF; text-align:left;} /*设置提示框内的li标签效果*/ |
现在才算是真正的完全制作完成,至于要不要设置一个延迟处理,或是其它更完善的功能,留给朋友们自己开动脑筋了,大家也可以在下面回复你的想法,等等。
客户端完整代码:
代码如下 | 复制代码 |
$('#search_auto').css({'width':$('#search input[name="k"]').width()+4}); }); |
服务器端完整代码:
代码如下 | 复制代码 |
$v=$_POST[value];
while($ro=mysql_fetch_array($re)) echo ' echo ' echo ' ?> |
方法二,使用jquery.input_complete插件了,这个非常简单
例
代码如下 | 复制代码 |
|
html只要简单的
代码如下 | 复制代码 |
tt" class="sel_quy" autocomplete="off" /> |
注意这里在里的ID必须TT哦,这个你可以自己定义名字,同时jquery.input_complete插件与jquery大家自己去下载吧。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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 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 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'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

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

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

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

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.
