当前位置:网站首页>搜索框字符自动补全
搜索框字符自动补全
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>
实现效果
边栏推荐
- 特别数的和
- 企业员工人事管理系统(数据库课设)
- Golang: go to connect and use mysql
- 小白的0基础教程SQL: 什么是SQL 01
- 目标检测概述-上篇
- 爬虫基本原理介绍、实现以及问题解决
- Information system project managers must recite the work of the core test site (56) Configuration Control Board (CCB)
- 特殊的日子,值得纪念
- 【一句话攻略】彻底理解JS中的回调(Callback)函数
- How to use Photoshop to composite star trail photos, post-processing method of night sky star trail photos
猜你喜欢
曲柄滑块机构运动分析和参数优化
The BP neural network based on MATLAB voice characteristic signal classification
matlab 风速模型 小波滤波
「面经分享」西北大学 | 字节 生活服务 | 一面二面三面 HR 面
特殊的日子,值得纪念
第02章 MySQL的数据目录【1.MySQL架构篇】【MySQL高级】
Practical training Navicat Chinese and English mode switching
Zero-code website development tool: WordPress
Upgrade to heavyweight lock, lock reentrancy will lead to lock release?
special day to remember
随机推荐
my creative day
基于MATLAB的BP神经网络进行语音特征信号分类
LeetCode 0149. Maximum number of points on a line
根据指定区域内容生成图片并进行分享总结
如何使用Photoshop合成星轨照片,夜空星轨照片后期处理方法
日志导致线程Block的这些坑,你不得不防
从购买服务器到网站搭建成功保姆级教程~超详细
Vim简介
并发编程13-JUC之CountDownLatch
LeetCode Question of the Day (309. Best Time to Buy and Sell Stock with Cooldown)
NIO programming
flinkcdc对mysql的date字段类型转化有什么解决思路么
Offer刷题——1
Introduction to the basic principles, implementation and problem solving of crawler
Windows taskbar icon abnormal solution
Dart exception details
Why is the lightweight VsCode used more and more?Why eat my C drive 10G?How to Painlessly Clean VsCode Cache?Teach you how to lose weight for C drive
曲柄滑块机构运动分析和参数优化
LeetCode 0150. 逆波兰表达式求值
curl (7) Failed connect to localhost8080; Connection refused