jQuery UI,简而言之,它是一个基于jQuery的前端UI框架。我们可以使用jQuery + jQuery UI非常简单方便地制作出界面美观、功能强大、跨浏览器兼容的前端html界面。

  Autocomplete,是一个功能强大的自动完成输入的jQuery插件,它也是jQuery UI的一部分。相信用过百度或者Google搜索的读者一定不会陌生,当我们在搜索框中开始输入搜索的关键字时,搜索引擎就会智能地帮我们联想并匹配我们所需的搜索关键字内容。

  一、首先了解下JQueryUI提供的重要属性

  1. autoFocus:当智能提示框出现时,是否自动选中第一项,默认为false,即不选中。  

  2. delay:在按键后执行搜索的延时,默认为300ms。

  3. disabled:是否禁用自动完成功能,默认为false。

  4. minLength:触发自动完成功能需要输入的最小字符数量。

  5. source:即为指定智能提示下拉框中的数据来源,支持三种类型。

    ① Array,主要用于本地化数据提供,支持两种格式:字符串数组 [ "Choice1", "Choice2" ]及标签和值属性的Json格式数组 [ { label: "Choice1", value: "value1" }, ... ]

    ② String,用于ajax请求的远程地址链接,返回Array或Json格式字符串。

    ③ Function,回调函数,最具有灵活性的一种方式,可用于返回任何数据源方式来实现自动完成,其中包含两个参数requestresponse通过request.term来获取用户输入的值,通过response(argument)来对获取的数据源进行显示。

  二、JQuery UI还提供了一些有用的方法

  1. close():关闭智能提示选择框。

  2. destroy():销毁智能提示选择框,将其所产生的元素完全删除,使其恢复至初始状态。

  3. disable():禁用自动完成功能。

  4. enable():开启自动完成功能。

  三、主要事件包括

  1. change(event, ui):当值改变时发生,ui.item为选中的项。

  2. close(event, ui):当智能提示框关闭时发生。

  3. create(event, ui):当智能提示框创建时发生,可以在此事件中,对外观进行一些控制。

  4. focus(event, ui):当智能提示列表任意一项获得焦点时发生,ui.item为获得焦点的项。

  5. open(event, ui):当智能提示框打开或更新时发生。

  6. response(event,ui):在搜索完成后智能提示框显示前发生,可以在此事件中对显示项进行处理

  7. search(event, ui):在开始请求之前发生,可以在此事件中返回false来取消请求。

  8. select(event, ui): 当智能提示框中任意一项被选中时发生,ui.item为选中的项。

  示例一(基于本地数据,来源于官方demo)

 <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete - 本地数据</title>
<link rel="stylesheet" href="jquery-ui.min.css">
<script src="jquery-1.9.0.min.js" type="text/javascript"></script>
<script src="jquery-ui.min.js" type="text/javascript"></script>
<style>
.ui-autocomplete {
max-height: 100px;
overflow-y: auto;
/* prevent horizontal scrollbar */
overflow-x: hidden;
}
/* IE 6 doesn't support max-height
* we use height instead, but this forces the menu to always be this tall
*/
* html .ui-autocomplete {
height: 100px;
}
</style>
</head>
<body>
<div class="ui-widget">
<label for="tags">请输入: </label>
<input id="tags">
</div>
</body>
<script>
$(function() {
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
"C",
"C++",
"Clojure",
"COBOL",
"ColdFusion",
"Erlang",
"Fortran",
"Groovy",
"Haskell",
"Java",
"JavaScript",
"Lisp",
"Perl",
"PHP",
"Python",
"Ruby",
"Scala",
"Scheme"
];
$( "#tags" ).focus(function(){
$( "#tags" ).autocomplete({
source: availableTags
});
}); });
</script>
</html>

效果图如下:

  示例二(基于远程数据,来源于官方demo)

 <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete - 远程数据</title>
<link rel="stylesheet" href="jquery-ui.min.css">
<script src="jquery-1.9.0.min.js" type="text/javascript"></script>
<script src="jquery-ui.min.js" type="text/javascript"></script>
<style>
.ui-autocomplete-loading {
background: white url('images/ui-anim_basic_16x16.gif') right center no-repeat;
}
</style>
<script>
$(function() {
function log( message ) {
$( "<div>" ).text( message ).prependTo( "#log" );
$( "#log" ).scrollTop( 0 );
} $( "#birds" ).autocomplete({
source: "search.php",
minLength: 2,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.value + " aka " + ui.item.id :
"Nothing selected, input was " + this.value );
}
});
});
</script>
</head>
<body> <div class="ui-widget">
<label for="birds">请输入: </label>
<input id="birds">
</div> <div class="ui-widget" style="margin-top:2em; font-family:Arial">
结果:
<div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div>
</div> </body>
</html>

对应的后台查询的php代码如下:

 <?php

 sleep( 3 );
// no term passed - just exit early with no response
if (empty($_GET['term'])) exit ;
$q = strtolower($_GET["term"]);
// remove slashes if they were magically added
if (get_magic_quotes_gpc()) $q = stripslashes($q); $items = array(
"Great Bittern"=>"Botaurus stellaris",
"Little Grebe"=>"Tachybaptus ruficollis",
"Black-necked Grebe"=>"Podiceps nigricollis",
"Little Bittern"=>"Ixobrychus minutus",
"Black-crowned Night Heron"=>"Nycticorax nycticorax",
); $result = array();
foreach ($items as $key=>$value) {
if (strpos(strtolower($key), $q) !== false) {
array_push($result, array("id"=>$value, "label"=>$key, "value" => strip_tags($key)));
}
if (count($result) > 11)
break;
} // json_encode is available in PHP 5.2 and above, or you can install a PECL module in earlier versions
echo json_encode($result); ?>

  注意:当我们从后台获取数据时,如果没有其他额外的过滤操作,Autocomplete会将过滤的操作交给服务器后台来完成。在发送ajax请求时,Autocomplete会把当前输入框中的文字以默认参数名term的形式追加到我们设置的URL地址后面。当我们输入一个c时,Autocomplete实际发送的请求路径为/ajax-actions.php?term=c。因此,Autocomplete认为服务器返回的数据就是过滤后需要显示的数据。

参考资料:

http://www.365mini.com/page/jquery-ui-autocomplete.htm

http://www.cnblogs.com/psforever/archive/2013/03/02/2940124.htm

http://blog.csdn.net/sadfishsc/article/details/7572054

http://www.cnblogs.com/lwme/archive/2012/02/12/jquery-ui-autocomplete.html

最新文章

  1. [LeetCode] Populating Next Right Pointers in Each Node II 每个节点的右向指针之二
  2. java高薪之路__009_网络
  3. Mac OS X使用快捷键改善窗口管理的六个方法
  4. 使用mybatis操作mysql数据库SUM方法返回NULL解决
  5. 快速配置 Samba 将 Linux 目录映射为 Windows 驱动器,用于跨平台编程
  6. Struts2 - Action no cache
  7. windows下游戏服务器端框架Firefly安装说明及demo运行
  8. 基于KVM建立虚拟机的步骤及总结说明
  9. uva 620 Cellular Structure
  10. 决策树(C4.5)原理
  11. 学习python的*args和 **kwargs
  12. 数据库之Oracle——初级
  13. c#实现Word转换PNG图片
  14. 关于Java的移位运算符
  15. ANDROID框架结构和介绍
  16. 1、Linux下部署NetCore应用
  17. BZOJ4668: 冷战 [并查集 按秩合并]
  18. Linux常用命令详解(week1_day1_3)--技术流ken
  19. vue_组件间通信:自定义事件、消息发布与订阅、槽
  20. shell脚本三——正则表达式

热门文章

  1. @Java类加载器及双亲委派模型
  2. 新买的mac笔记本,发现vi编辑器没有颜色的解决方案
  3. applicationContext.xml中的使用${}是代表什么意思?
  4. Median of Two Sorted Array leetcode java
  5. 微软 WCF的几种寄宿方式,寄宿IIS、寄宿winform、寄宿控制台、寄宿Windows服务
  6. Java基础(五):数组和Java方法
  7. Cognos中新建SQLserver数据源的步骤
  8. [Debug] Debug Node.js Application by using Chrome Dev tools
  9. WebService SOAP、Restful和HTTP(post/get)请求区别
  10. 提高ASP.NET首页性能的十大方法