当前位置:网站首页>搜索框字符自动补全
搜索框字符自动补全
2022-08-01 07:07:00 【mountisome】
jQuery Autocomplete
jQuery Autocomplete插件根据用户输入值进行搜索和过滤,让用户快速找到并从预设值列表中选择。
我用的是别人在Autocomplete插件基础之上改造的一个插件。
从上面链接下载代码即可。
具体代码
使用之前引入jQuery文件,我使用的是jquery-3.6.0.min.js
。
css和js代码就是上面链接中下载的代码。
需要注意的是:
- 为了防止前端发送过多请求到后端,必须使用JavaScript函数节流来限制发送请求的数目。
- 以上的数据是提前在前端用数组保存的,如果想要及时地传递数据,就要用到AJAX异步从前端发送数据到后端,然后后端将数据封装好再返回到前端。
JavaScript函数节流
概念:限制一个函数在一定时间内只能执行一次。
如果不做函数节流的话,可能会造成程序运行速度降低,甚至卡死崩溃。重复的AJAX请求也可能导致数据混乱,造成网络拥塞。
代码:
function throttle(method,delay,duration){
let timer=null, begin=new Date();
return function(){
let context=this, args=arguments, current=new Date();
clearTimeout(timer);
if(current-begin>=duration){
method.apply(context,args);
begin=current;
}else{
timer=setTimeout(function(){
method.apply(context,args);
},delay);
}
}
}
然后对键盘按键松开事件调用函数:
window.onkeyup = throttle(check,100,200); // check函数是调用ajax请求
AJAX请求
具体可以看我的这篇文章。
我的js部分代码具体实现如下:
<script th:inline="javascript">
let data = []; // 保存从后端传递过来的数据
let autoComplete = new AutoComplete('subTitle','auto',data);
function check() {
let subTitle = document.getElementById("subTitle").value; // 搜索框中的内容
if (subTitle != null && subTitle !== "" && typeof (event) != "undefined" && event.keyCode !== 13 && event.keyCode !== 38 && event.keyCode !== 40) {
// ajax请求
jQuery.ajax({
url: "/user/findSubtitlesByName",
type: "post",
async : false,
data: {
"subTitle": subTitle
},
dataType: "JSON",
success: function(data) {
autoComplete.value_arr = data;
},
error: function(errorMsg) {
console.log(errorMsg);
}
});
}
autoComplete.start(event);
}
// 函数节流
function throttle(method,delay,duration){
let timer=null, begin=new Date();
return function(){
let context=this, args=arguments, current=new Date();
clearTimeout(timer);
if(current-begin>=duration){
method.apply(context,args);
begin=current;
}else{
timer=setTimeout(function(){
method.apply(context,args);
},delay);
}
}
}
window.onkeyup = throttle(check,100,200);
</script>
实现效果
边栏推荐
- Introduction to the basic principles, implementation and problem solving of crawler
- 从零开始—仿牛客网讨论社区项目(一)
- Golang: go to connect and use mysql
- 牛客刷SQL---2
- 对于升级go1.18的goland问题
- 爬虫基本原理介绍、实现以及问题解决
- 我三本学历,五面阿里,被面试官“供”着出来了,拿了33*15的Offer
- Json对象和Json字符串的区别
- Windows taskbar icon abnormal solution
- 旋度(7)连接失败localhost8080;连接拒绝了
猜你喜欢
爆肝3万字,最硬核丨Mysql 知识体系、命令全集 【建议收藏 】
爬虫框架 Scrapy 详解
The log causes these pits in the thread block, you have to prevent
MySQL row locks and gap locks
如何使用Photoshop合成星轨照片,夜空星轨照片后期处理方法
I have three degrees, and I have five faces. I was "confessed" by the interviewer, and I got an offer of 33*15.
How to use Photoshop to composite star trail photos, post-processing method of night sky star trail photos
太厉害了,终于有人能把文件上传漏洞讲的明明白白了
仿牛客网项目总结
Explosive 30,000 words, the hardest core丨Mysql knowledge system, complete collection of commands [recommended collection]
随机推荐
测试工具(四)Jenkins环境搭建与使用
Electromagnetic compatibility introductory tutorial (6) test project
2022.7.27 Selected lectures on good topics
Detailed explanation of the crawler framework Scrapy
【视觉SLAM十四讲】第一章理论详解
LeetCode 415:字符串相加
【南瓜书ML】(task4)神经网络中的数学推导(更新ing)
Golang: go to connect and use mysql
金山打字通 官网 下载
dbeaver连接MySQL数据库及错误Connection refusedconnect处理
目标检测概述-上篇
LeetCode 0150. Reverse Polish Expression Evaluation
Matlab simulink particle swarm optimization fuzzy pid control motor pump
基于百度OCR的网站验证码在线识别
LeetCode240+312+394
LeetCode 0149. 直线上最多的点数
Golang: go open web service
阿里云李飞飞:中国云数据库在很多主流技术创新上已经领先国外
【一句话攻略】彻底理解JS中的回调(Callback)函数
Srping中bean的生命周期