html5单页面网站建设,网站搭建php打不开,如何在wordpress中加备案编号,广东网络seo推广平台jQuery UI Autocomplete#xff08;自动完成#xff09;实例
Autocomplete 是 jQuery UI 中非常实用的组件#xff0c;用于输入框提供智能建议列表#xff0c;支持本地数据、远程 AJAX 数据、分类显示、自定义渲染等。常用于搜索框、标签输入、地址补全等场景。
官方演示…jQuery UI Autocomplete自动完成实例Autocomplete是 jQuery UI 中非常实用的组件用于输入框提供智能建议列表支持本地数据、远程 AJAX 数据、分类显示、自定义渲染等。常用于搜索框、标签输入、地址补全等场景。官方演示地址https://jqueryui.com/autocomplete/下面提供几个渐进实例从基础到高级代码使用最新 CDN可直接复制到 HTML 文件测试。1.基础本地数据自动完成使用数组作为数据源。!DOCTYPEhtmlhtmlheadmetacharsetutf-8titlejQuery UI Autocomplete 基础示例/titlelinkrelstylesheethref//code.jquery.com/ui/1.13.2/themes/smoothness/jquery-ui.cssscriptsrc//code.jquery.com/jquery-3.6.0.min.js/scriptscriptsrc//code.jquery.com/ui/1.13.2/jquery-ui.min.js/script/headbodylabel编程语言:/labelinputidautocompletescript$(function(){varlanguages[JavaScript,Java,Python,C,C#,PHP,Ruby,Go,Swift,Kotlin];$(#autocomplete).autocomplete({source:languages,minLength:2// 输入至少2个字符才触发建议});});/script/body/html2.带标签的对象数据 分类显示使用对象数组支持label显示和value选中后填充值并实现分类。scriptvardata[{label:JavaScript,category:前端},{label:HTML,category:前端},{label:CSS,category:前端},{label:Java,category:后端},{label:Python,category:后端},{label:Node.js,category:后端}];$(#autocomplete).autocomplete({source:data,minLength:1,// 自定义分类菜单_renderMenu:function(ul,items){varthatthis;varcurrentCategory;$.each(items,function(index,item){if(item.category!currentCategory){ul.append(li classui-autocomplete-categoryitem.category/li);currentCategoryitem.category;}that._renderItemData(ul,item);});}});/scriptstyle.ui-autocomplete-category{font-weight:bold;background:#eee;padding:5px;}/style3.自定义渲染带图标或图片使用_renderItem自定义下拉项显示。script$(#autocomplete).autocomplete({source:[{label:Apple,icon:https://via.placeholder.com/30?textA},{label:Banana,icon:https://via.placeholder.com/30?textB},{label:Orange,icon:https://via.placeholder.com/30?textO}]}).data(ui-autocomplete)._renderItemfunction(ul,item){return$(li).append(divimg srcitem.icon stylewidth:30px;vertical-align:middle;margin-right:10px;item.label/div).appendTo(ul);};/script4.远程数据源AJAX从服务器动态获取数据模拟可用数据。script$(#autocomplete).autocomplete({source:function(request,response){$.ajax({url:https://api.example.com/search,// 替换为你的APIdata:{term:request.term},success:function(data){response($.map(data,function(item){return{label:item.name,value:item.id};}));}});},minLength:2,delay:300// 延迟请求防频繁输入});/script5.事件监听select、changescript$(#autocomplete).autocomplete({// ...select:function(event,ui){console.log(选中: ui.item.label (值: ui.item.value));},change:function(event,ui){if(!ui.item)console.log(未从列表中选择);}});/script小技巧minLength: 0可实现点击输入框即显示全部建议。支持多选Multiple values查看官方 “Multiple, remote” 示例。移动端友好默认支持触摸。如果你需要多选标签输入、结合 AJAX 从数据库搜索的完整后端示例或带加载动画的版本请告诉我更多需求