官网:http://jqueryui.com/autocomplete

最简单的形式:

var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
 
];
$( "#tags" ).autocomplete({
source: availableTags
});

数据源source有多种形式:

source:

Type: Array or String or FunctionObject request, Function response( Object data ) )

Default: none; must be specified
Defines the data to use, must be specified.

Independent of the variant you use, the label is always treated as text. If you want the label to be treated as html you can use Scott González' html extension. The demos all focus on different variations of the source option - look for one that matches your use case, and check out the code.

Multiple types supported:

  • Array: An array can be used for local data. There are two supported formats:

    • An array of strings: [ "Choice1", "Choice2" ]
    • An array of objects with label and value properties: [ { label: "Choice1", value: "value1" }, ... ]

    The label property is displayed in the suggestion menu. The value will be inserted into the input element when a user selects an item. If just one property is specified, it will be used for both, e.g., if you provide only valueproperties, the value will also be used as the label.

  • String: When a string is used, the Autocomplete plugin expects that string to point to a URL resource that will return JSON data. It can be on the same host or on a different one (must provide JSONP). The Autocomplete plugin does not filter the results, instead a query string is added with a term field, which the server-side script should use for filtering the results. For example, if the source option is set to "http://example.com" and the user types foo, a GET request would be made to http://example.com?term=foo. The data itself can be in the same format as the local data described above.
  • Function: The third variation, a callback, provides the most flexibility and can be used to connect any data source to Autocomplete. The callback gets two arguments:
    • request object, with a single term property, which refers to the value currently in the text input.(输入框的值) For example, if the user enters "new yo" in a city field, the Autocomplete term will equal "new yo".
    • response callback, which expects a single argument: the data to suggest to the user. This data should be filtered based on the provided term, and can be in any of the formats described above for simple local data. It's important when providing a custom source callback to handle errors during the request. You must always call the response callback even if you encounter an error. This ensures that the widget always has the correct state.

    When filtering data locally, you can make use of the built-in $.ui.autocomplete.escapeRegex function. It'll take a single string argument and escape all regex characters, making the result safe to pass to new RegExp().

Code examples:

Initialize the autocomplete with the source option specified:

1
$( ".selector" ).autocomplete({ source: [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ] });

Get or set the source option, after initialization:

var source = $( ".selector" ).autocomplete( "option", "source" );
 
// setter
$( ".selector" ).autocomplete( "option", "source", [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ] );、
 
这个很有用,有时我们想在初始化后改变source的值。向下面的例子中:
 var  Cache=[];

    var firstLoaded=true;
$(".input").autocomplete({
minLength:,
source: Cache
}).focus(function(){
var that=$(this); if(firstLoaded)
{
var ret=[];
$.getJSON('url',{},function(data){
for(var i in data)
{
ret.push(data[i]);
} Cache=ret;
firstLoaded=false; that.autocomplete({ source: Cache});//一定要重新载入
that.autocomplete('search'); });
}
else
{
that.autocomplete('search');
} });

我以为在初始化是

 source: Cache指向的是引用,ajax更改了cache的值应该会起作用的,可是事与愿违,必须,重新调用
that.autocomplete({ source: Cache});方可起作用。

上面的例子也演示了如何当input获得焦点时立即显示自动完成。这里有2个关键字。

focus函数,调用autocomplete触发search,还有一点是设置maxLength为0,因为search函数Triggered before a search is performed, after minLength and delay are met. If canceled, then no request will be started and no items suggested.

 

jquery ui autoComplete自动完成的更多相关文章

  1. jQuery UI Autocomplete是jQuery UI的自动完成组件(share)

    官网:http://jqueryui.com/autocomplete/ 以下分享自:http://www.cnblogs.com/yuzhongwusan/archive/2012/06/04/25 ...

  2. jQuery UI Autocomplete是jQuery UI的自动完成组件

    支持的数据源 jQuery UI Autocomplete主要支持字符串Array.JSON两种数据格式. 普通的Array格式没有什么特殊的,如下: ? 1 ["cnblogs" ...

  3. 可输入自动匹配Select——jquery ui autocomplete

    <!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  4. jQuery UI AutoComplete的使用

    现场提出优化单,Table Mapping里关于获取数据源下所有表名的地方由于表数量过多选择不便,需添加搜索功能.原本的实现是一个Dialog ,现打算将其改为AutoComplete. 框架使用的是 ...

  5. Jquery ui autocomplete简单api

    重要说明:与配置选项类似,Autocomplete插件的方法也不是直接调用,而且通过autocomplete()方法进行间接调用.例如: $("#title").autocompl ...

  6. jquery ui autocomplete 实现点击文本框,出现所有查询信息效果,与bootstrap结合使用修改样式

    直接看代码 <!doctype html> <html lang="en"> <head> <meta charset="utf ...

  7. jQuery ui autocomplete下拉列表样式失效解决,三种获取数据源方式,

    jQuery有很多很多的已经实现,很漂亮的插件,autocomplete就是其中之一.jQuery ui autocomplete主要支持字符串Array.JSON两种数据格式,jQuery ui b ...

  8. jQuery UI Autocomplete Combobox 配 ASP.NET DropDownList

    0.引言   1.起因                  一开始使用Autocomplete做了一个自动补全的文本框,如上图.后来因业务需要希望能在这个文本框的边上做个下拉列表按钮,一按就展开所有支持 ...

  9. jQuery ui autocomplete选择列表被Bootstrap模态窗遮挡的完美解决方法

    转:http://www.cnblogs.com/wiseant/p/4553837.html 最近在一个ASP.NET MVC5项目中使用Bootstrap的模态窗(弹出层)来让用户填写内容,其中的 ...

随机推荐

  1. 线程模式HS/HA和L/F的区别, HS/HA的实现原理图

    线程池模式一般分为两种:L/F领导者与跟随者模式.HS/HA半同步/半异步模式. HS/HA 半同步/ 半异步模式 :分为三层,同步层.队列层.异步层,又称为生产者消费者模式,主线程处理I/O事件并解 ...

  2. java基础-基础类型包装类型

    想要对基本类型数据进行更多的操作,最方便的方式就是将其封装成对象. 为啥呢?因为在对象描述中就可以定义更多的属性和行为对该基本数据类型进行操作. [八种基本数据类型的包装类] byte --Byte ...

  3. JBoss部署项目log4j配置会造成死锁问题,浏览器访问一直pending状态

    今天将项目部署到JBoss服务器上,部署成功后,浏览器访问页面一直在等待响应. 查了很长时间,最后在服务器上通过jstack pid命令查看Java堆栈信息,发现了有两个线程死锁. 看到造成死锁的原因 ...

  4. node.js:怎样同时执行多条SQLs,且只有一个回调

    本文主要介绍开源node.js库mysql-queries,其可以同时执行多条SQLs,且只有一个回调.同时抛砖引玉,与大家交流node.js开发经验. node.js很大的特点就是事件驱动.非阻塞和 ...

  5. ValueError: No JSON object could be decoded?此种异常的解决方案之一

    第一次遇到这样的异常,实在不知道如何是好?进行了测试发现报错的json出没有问题,而且每次出现异常的位置不一样 于是我认为这样的问题可能是因为程序执行过快,所以很简单的解决办法是: def deal_ ...

  6. asp.net mvc 伪静态路由配置

    asp.net mvc实现伪静态路由必须按如下方式设置好,才能访问 .htm 或者.html页面 C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspne ...

  7. jdbc mysql 取数,突然取不到数据,数据库中有数据

    项目用的是jdbc+mysql,局网取数据的时候,数据一切正常,但是传到服务器上以后,曾经是好的 不知道为什么,近期一传就取不到数据,发现android写的也没有问题,至少大体上没有语法问题. 跟踪后 ...

  8. web sevice 生成代理类及使用

    一.生成代理类: VS2008下这样写 wsdl.exe /l:cs /out:D:/ProxyServices.cs http://localhost/WebService.asmx VS2010下 ...

  9. poj 2728 Desert King (最小比例生成树)

    http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissio ...

  10. 3140:[HNOI2013]消毒 - BZOJ

    题目描述 Description 最近在生物实验室工作的小 T 遇到了大麻烦. 由于实验室最近升级的缘故,他的分格实验皿是一个长方体,其尺寸为 a*b*c,a.b.c均为正整数.为了实验的方便,它被划 ...