search
HomeWeb Front-endJS TutorialDetailed explanation of the usage of text input auto-completion function in jQuery UI library_jquery

Autocomplete is a UI tool that reduces the need for users to enter complete information. Usually
Enter your email address, search keywords, etc., and then extract the corresponding complete string for the user to select.

1. Call the autocomplete() method

$('#email').autocomplete({ 
 
  source : ['aaa@163.com', 'bbb@163.com', 'ccc@163.com'], 
 
}); 

2. Modify autocomplete() style
Since the autocomplete() method is a pop-up window, then the mouse hover style. Want to get
through Firebug For the style of the background when hovering, you can directly find the corresponding CSS in jquery.ui.css.

//无须修改ui 里的CSS,直接用style.css 替代掉 
.ui-menu-item a.ui-state-focus { 
 
  background:url(../img/xxx.png); 
 
} 

3. Attributes of autocomplete() method
There are two forms of automatic completion methods: 1.autocomplete(options), options are object key-value pairs
Pass parameters in the form, each key-value pair represents an option; 2.autocomplete('action', param), action
It is a string that operates the dialog box method, and param is an option in options.

autocomplete appearance options
Attribute
autocomplete 外观选项
属性
默认值/类型
说明
disabled 
false/布尔值
设置为true,将禁止显示自动补全。
source 
无/数组
指定数据源,可以是本地的,也可以是远程的。
minLength 
1/数值
默认为1,触发补全列表最少输入字符数。
delay 
300/数值
默认为300 毫秒,延迟显示设置。
autoFocus 
false/布尔值
设置为true 时,第一个项目会自动被选定。
Default value/type Description
disabled false/Boolean Set to true to disable auto-completion.
source None/Array Specify the data source, which can be local or remote.
minLength 1/value Default is 1, which is the minimum number of characters required to trigger the completion list.
delay 300/value Default is 300 milliseconds, delay display setting.
autoFocus false/Boolean When set to true, the first item will be automatically selected.

$('#email').autocomplete({ 
 
  source : ['aaa@163.com', 'bbb@163.com', 'ccc@163.com'], 
 
  disabled : false, 
 
  minLength : 2, 
 
  delay : 50, 
 
  autoFocus : true, 
 
}); 

autocomplete 页面位置选项
属性
默认值/类型
说明
position
无/对象
使用对象的键值对赋值,有两个属性:my 和at
表示坐标。my 是以目标点左上角为基准,at 以
目标点右下角为基准。

$('#email').autocomplete({ 
 
  position : { 
 
   my : 'left center', 
 
   at : 'right center' 
 
  } 
 
}); 

四.autocomplete()方法的事件
   除了属性设置外,autocomplete()方法也提供了大量的事件。这些事件可以给各
种不同状态时提供回调函数。这些回调函数中的this 值等于对话框内容的div 对象,不
是整个对话框的div。
autocomplete 事件选项

autocomplete 事件选项
事件名
说明
create
当自动补全被创建时会调用create 方法,该方法有两个
参数(event, ui)。此事件中的ui 参数为空。
open
当自动补全被显示时,会调用open 方法,该方法有两个
参数(event, ui)。此事件中的ui 参数为空。
close
当自动补全被关闭时,会调用close 方法,该方法有两个
参数(event, ui)。此事件中的ui 参数为空。
focus
当自动补全获取焦点时,会调用focus 方法,该方法有两
个参数(event, ui)。此事件中的ui 有一个子属性对象item,
分别有两个属性:label,补全列表显示的文本;value,
将要输入框的值。一般label 和value 值相同。
select
当自动补全获被选定时,会调用select 方法,该方法有两
个参数(event, ui)。此事件中的ui 有一个子属性对象item,
分别有两个属性:label,补全列表显示的文本;value,
将要输入框的值。一般label 和value 值相同。
change
当自动补全失去焦点且内容发生改变时,会调用change
方法,该方法有两个参数(event, ui)。此事件中的ui 参数
为空。
search
当自动补全搜索完成后,会调用search 方法,该方法有
两个参数(event, ui)。此事件中的ui 参数为空。
response
当自动补全搜索完成后,在菜单显示之前,会调用
response 方法,该方法有两个参数(event, ui)。此事件中
的ui 参数有一个子对象content,他会返回label 和value
值,可通过遍历了解。

$('#email').autocomplete({ 
 
  source : ['aaa@163.com', 'bbb@163.com', 'ccc@163.com'], 
 
  disabled : false, 
 
  minLength : 1, 
 
  delay : 0, 
 
  focus : function (e, ui) { 
 
   ui.item.value = '123'; 
 
  }, 
 
  select : function (e, ui) { 
 
    ui.item.value = '123'; 
 
  }, 
 
  change : function (e, ui) { 
 
   alert(''); 
 
  }, 
 
  search : function (e, ui) { 
 
   alert(''); 
 
  }, 
 
}); 
autocomplete('action', param)方法

<table border="1" cellspacing="1" cellpadding="1" width="700" height="500"><caption>autocomplete('action', param)方法</caption><tbody><tr><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px">方法<br /></span></td><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px">返回值<br /></span></td><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px">说明<br /></span></td></tr><tr><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px">autocomplete('close')<br /></span></td><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px"> jQuery 对象<br /></span></td><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px">关闭自动补齐<br /></span></td></tr><tr><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px">autocomplete('disable')<br /></span></td><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px"> jQuery 对象<br /></span></td><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px">禁用自动补齐<br /></span></td></tr><tr><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px">autocomplete('enable')<br /></span></td><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px"> jQuery 对象<br /></span></td><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px">启用自动补齐<br /></span></td></tr><tr><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px">autocomplete('destroy')<br /></span></td><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px"> jQuery 对象<br /></span></td><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px">删除自动补齐,直接阻断<br /></span></td></tr><tr><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px">autocomplete('widget')<br /></span></td><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px"> jQuery 对象<br /></span></td><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px">获取工具提示的jQuery 对象<br /></span></td></tr><tr><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px">autocomplete('search',value)<br /></span></td><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px"> jQuery 对象<br /></span></td><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px">在数据源获取匹配的字符串<br /></span></td></tr><tr><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px">autocomplete('option', param) <br /></span></td><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px">一般值<br /></span></td><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px">获取options 属性的值<br /></span></td></tr><tr><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px">autocomplete('option', param,value)<br /></span></td><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px"> jQuery 对象<br /></span></td><td style="text-align: center"><span style="font-family: Courier New; font-size: 14px">设置options 属性的值</span></td></tr></tbody></table>

$('#reg').on('autocompleteopen', function () { 
 
  alert('打开时触发!'); 
 
}); 

五、邮箱自动补全
  通过自动补全source 属性的function 回调函数,来动态的设置数据源,以达到可以
实现邮箱补全功能。

1.数据源function
   自动补全UI 的source 不但可以是数组,也可以是function 回调函数。提供了自带的
两个参数设置动态的数据源。

$('#email').autocomplete({ 
 
  source : function (request, response) { 
 
   alert(request.term);    //可以获取你输入的值 
 
   response(['aa', 'aaaa', 'aaaaaa', 'bb']);    //展示补全结果 
 
  }, 
 
}); 

注意:这里的response 不会根据你搜索关键字而过滤无关结果,而是把整个结果全部呈现出
来。因为source 数据源,本身就是动态改变的,就由自定义,从而放弃系统内置的搜索能力。

2.邮箱自动补全

$('#email').autocomplete({ 
 
  autoFocus : true, 
 
  delay : 0, 
 
  source : function (request, response) { 
 
   var hosts = ['qq.com','163.com', '263.com', 'gmail.com', 'hotmail.com'],    //起始 
 
     term = request.term,    //获取输入值 
 
     ix = term.indexOf('@'),    //@ 
 
     name = term,    //用户名 
 
     host = '',    //域名  
 
     result = [];    //结果 
 
   //结果第一条是自己输入 
   result.push(term); 
 
   if (ix > -1) {    //如果有@的时候 
 
     name = term.slice(0, ix);    //得到用户名 
 
     host = term.slice(ix + 1);    //得到域名 
 
   } 
 
   if (name) { 
 
     //得到找到的域名 
     var findedHosts = (host &#63; $.grep(hosts, function (value, index) { 
 
                 return value.indexOf(host) > -1; 
 
               }) : hosts), 
 
     //最终列表的邮箱 
     findedResults = $.map(findedHosts, function (value, index) { 
 
              return name + '@' + value; 
 
             }); 
 
     //增加一个自我输入 
     result = result.concat(findedResults); 
 
   } 
 
   response(result); 
 
  }, 
 
}); 
Statement
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
Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.