jquery ui autoComplete自动完成
官网:http://jqueryui.com/autocomplete
最简单的形式:
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
];
$( "#tags" ).autocomplete({
source: availableTags
});
数据源source有多种形式:
source:
Type: Array or String or Function( Object request, Function response( Object data ) )
none; must be specifiedIndependent 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
labelandvalueproperties:[ { 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. - An array of strings:
- 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
termfield, which the server-side script should use for filtering the results. For example, if thesourceoption is set to"http://example.com"and the user typesfoo, a GET request would be made tohttp://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:
- A
requestobject, with a singletermproperty, 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". - A
responsecallback, 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 theresponsecallback 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.escapeRegexfunction. It'll take a single string argument and escape all regex characters, making the result safe to pass tonew RegExp(). - A
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" ] );、 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自动完成的更多相关文章
- jQuery UI Autocomplete是jQuery UI的自动完成组件(share)
官网:http://jqueryui.com/autocomplete/ 以下分享自:http://www.cnblogs.com/yuzhongwusan/archive/2012/06/04/25 ...
- jQuery UI Autocomplete是jQuery UI的自动完成组件
支持的数据源 jQuery UI Autocomplete主要支持字符串Array.JSON两种数据格式. 普通的Array格式没有什么特殊的,如下: ? 1 ["cnblogs" ...
- 可输入自动匹配Select——jquery ui autocomplete
<!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- jQuery UI AutoComplete的使用
现场提出优化单,Table Mapping里关于获取数据源下所有表名的地方由于表数量过多选择不便,需添加搜索功能.原本的实现是一个Dialog ,现打算将其改为AutoComplete. 框架使用的是 ...
- Jquery ui autocomplete简单api
重要说明:与配置选项类似,Autocomplete插件的方法也不是直接调用,而且通过autocomplete()方法进行间接调用.例如: $("#title").autocompl ...
- jquery ui autocomplete 实现点击文本框,出现所有查询信息效果,与bootstrap结合使用修改样式
直接看代码 <!doctype html> <html lang="en"> <head> <meta charset="utf ...
- jQuery ui autocomplete下拉列表样式失效解决,三种获取数据源方式,
jQuery有很多很多的已经实现,很漂亮的插件,autocomplete就是其中之一.jQuery ui autocomplete主要支持字符串Array.JSON两种数据格式,jQuery ui b ...
- jQuery UI Autocomplete Combobox 配 ASP.NET DropDownList
0.引言 1.起因 一开始使用Autocomplete做了一个自动补全的文本框,如上图.后来因业务需要希望能在这个文本框的边上做个下拉列表按钮,一按就展开所有支持 ...
- jQuery ui autocomplete选择列表被Bootstrap模态窗遮挡的完美解决方法
转:http://www.cnblogs.com/wiseant/p/4553837.html 最近在一个ASP.NET MVC5项目中使用Bootstrap的模态窗(弹出层)来让用户填写内容,其中的 ...
随机推荐
- windows下nginx以服务自启动
1,下载最新版的 Windows Service Wrapper 程序,例如:"winsw-1.9-bin.exe" 也可以修改它的名字,例如:myapp.exe 2, 将重命名后 ...
- MySql中,复制旧表结构到新表
# 创建学生表 create table student(age int,name varchar(32))engine myisam charset utf8;insert into student ...
- 解决ionic在ios无法使用focus,ios focus失效的问题
最近也偷懒,很久没有写博客了.今天在项目中遇到了这个奇葩的问题,基于ionic的ios的hybird APP 无法使用focus()获取焦点和键盘的问题. 问题:基于ionic的ios的hybird ...
- 如何用命令的方式查看你的Office2010密钥是否是永久的有效
首先,ctrl+R , 然后输入cmd, 回车, 进入黑框框 其次,在你的office安装位置下找到这个文件OSPP.VSB,对其右键,查看其属性,复制下它的位置.,接着 就照着下图上的操作吧~ ...
- 如何使用Git——(二)
8.首次安装git,然后要把创建好的仓库放到github上,首先,你要先做一些基本设置,继续设置吧~~. 在git黑框框中输入 ssh-keygen -t rsa -C "your ema ...
- 解决java.io.IOException: Cannot run program "cygpath": CreateProcess error=2, 系统找不到指定的文件 的错误
一.外部环境: 系统环境:Windows 8 磁盘分区:只有C盘 开发环境:IntelliJ IDEA Community Edition 2016.1.3(64) 执行代码:rdd.saveAsTe ...
- Catalyst揭秘 Day8 Final 外部数据源和缓存系统
Catalyst揭秘 Day8 Final 外部数据源和缓存系统 今天是Catalyst部分的收官,主要讲一些杂项内容. 外部数据源处理 什么叫外部数据源,是SparkSql自己支持的一些文件格式,以 ...
- WPF 多线程处理(2)
WPF 多线程处理(1) WPF 多线程处理(2) WPF 多线程处理(3) WPF 多线程处理(4) WPF 多线程处理(5) WPF 多线程处理(6) WPF UI 设计需要自动适应窗体大小,那么 ...
- 闭包(Closures)
浅析 JavaScript 中的闭包(Closures) 一.前言 对于 JavaScript 来说,闭包是一个非常强大的特征.但对于刚开始接触的初学者来说它又似乎是特别高深的.今天我们一起来揭开闭包 ...
- 进入 App Store 打分
很多用户用了好软件后忘记或嫌麻烦而不去 App Store 进行打分评星,为此开发者可以在应用中加入打分按钮,点击后直接跳转到 App Store 的评分界面. App Store 上评论的链接地址是 ...