jQury下的自动补全及插件jquery.autocomplete.min.js的使用

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<head>

    <title>jQuery AutoComplete PlugInTest</title>

    <%@pagecontentType="text/html;charset=UTF-8" language="java" %>

    <%@pageisELIgnored="false"%>

    <metahttp-equiv="Content-Type" content="text/html;charset=UTF-8">

 

    <link rel="stylesheet"type="text/css" href="${ctx}/css/jquery.autocomplete.css"/>

 

 

    <script type="text/javascript"src="${ctx}/js/jquery/jquery-1.7.2.min.js"></script>

    <script type="text/javascript"src="${ctx}/js/jquery/jquery.autocomplete.min.js"></script>

 

 

    <script type="text/javascript">

 

       jQuery(document).ready(function(){

            var emails = [

                {name: "Peter Pan", to: "peter@pan.de" },

                {name: "Molly", to: "molly@yahoo.com" },

                {name: "Forneria Marconi", to: "live@japan.jp" },

                {name: "Master <em>Sync</em>", to:"205bw@samsung.com" },

                {name: "Dr. <strong>Tech</strong> de Log", to:"g15@logitech.com" },

                {name: "Don Corleone", to: "don@vegas.com" },

                {name: "Mc Chick", to: "info@donalds.org" },

                {name: "Donnie Darko", to: "dd@timeshift.info" },

                {name: "Quake The Net", to: "webmaster@quakenet.org" },

                {name: "Dr. Write", to: "write@writable.com" },

                {name: "GG Bond", to: "Bond@qq.com" },

                {name: "北京", to:"beijing@163.com" },

                {name: "Zhuzhu Xia", to: "zhuzhu@qq.com" },

                {name: "北平", to:"beiping@163.com" },

                {name: "北漂", to:"beipiao@163.com" },

                {name: "北魏", to:"beiwei@163.com" }

            ];

 

           $('#keyword').autocomplete(emails, {

               max: 8, //列表里的条目数

               minChars: 0,  //自动完成激活之前填入的最小字符

                width:200,   //提示的宽度,溢出隐藏

               scrollHeight: 200, //提示的高度,溢出显示滚动条

               matchCase:false,  //是否区分大小写

               matchContains: false, //包含匹配,就是data参数里的数据,是否只要包含文本框里的数据就显示

               autoFill: false, //自动填充

               formatItem: function(row, i, max) {

                   return i + '/' + max + ':"' + row.name + '"[' + row.to+ ']';

               },

               formatMatch: function(row, i, max) {

//                   return row.name + row.to;

                   return row.name;

               },

               formatResult: function(row) {

                   return row.to;

                }

           }).result(function(event, row, formatted) {

                       $("#content").val(row.to);

                   });

 

        });

 

    </script>

</head>

<body>

 

 

        <div>

            模糊匹配:

            <inputtype="text" id="keyword" />

           &nbsp;-->&nbsp;

            <inputtype="text" id="content" />

              </div>

 

</body>

</html>

 

--------------------------------------------------------------------------------------------------

修改jquery.autocompletet.css文件代码例子:

.ac_results {

padding: 0px;

    border: 1px solid #7F9DB9;  //显示的下拉框的边颜色

    background-color: #FFFFD9;   //初始化格式默认背景色

overflow: hidden;

z-index: 99999;

}

 

.ac_results ul {

width: 100%;

list-style-position: outside;

list-style: none;

padding: 0;

margin: 0;

}

 

.ac_results li {

margin: 0px;

padding: 2px 5px;

cursor: default;

display: block;

//if width will be 100% horizontalscrollbar will apear when scroll mode will be used

//width: 100%;

font: menu;

font-size: 12px;

//it is very important, if line-height notsetted or setted in relative units scroll will 

        //be broken infirefox

line-height: 16px;

overflow: hidden;

}

 

.ac_loading {

background: white url('/images/indicator.gif') rightcenter no-repeat;

}

 

.ac_odd {

    //background-color:#eee;

    //background-color: #FFF3B5;

    background-color: #DDDDDD;   //初始化格式默认背景色(隔行变色)

}

 

.ac_over {

//background-color: #0A246A;

    background-color: #196AC5;   //鼠标经过时的颜色

    color: white;  //鼠标经过的字体颜色

}

--------------------------------------------------------------------------------------------------

 

中文版使用说明】:

参数说明:

* minChars (Number):

在触发autoComplete前用户至少需要输入的字符数.Default:1,如果设为0,在输入框内双击或者删除输入框内内容时显示列表

* width (Number):

指定下拉框的宽度. Default: input元素的宽度

* max (Number):

autoComplete下拉显示项目的个数.Default: 10

* delay (Number):

击键后激活autoComplete的延迟时间(单位毫秒).Default: 远程为400 本地10

* autoFill (Boolean):

要不要在用户选择时自动将用户当前鼠标所在的值填入到input框. Default:false

* mustMatch (Booolean):

如果设置为true,autoComplete只会允许匹配的结果出现在输入框,所有当用户输入的是非法字符时将会得不到下拉框.Default: false

* matchContains (Boolean):

决定比较时是否要在字符串内部查看匹配,如ba是否与foo bar中的ba匹配.使用缓存时比较重要.不要和autofill混用.Default:false

* selectFirst (Boolean):

如果设置成true,在用户键入tab或return键时autoComplete下拉列表的第一个值将被自动选择,尽管它没被手工选中(用键盘或鼠标).当然如果用户选中某个项目,那么就用用户选中的值. Default: true

* cacheLength (Number):

缓存的长度.即对从数据库中取到的结果集要缓存多少条记录.设成1为不缓存.Default: 10

* matchSubset (Boolean):

autoComplete可不可以使用对服务器查询的缓存,如果缓存对foo的查询结果,那么如果用户输入foo就不需要再进行检索了,直接使用缓存.通常是打开这个选项以减轻服务器的负担以提高性能.只会在缓存长度大于1时有效.Default:true

* matchCase (Boolean):

比较是否开启大小写敏感开关.使用缓存时比较重要.如果你理解上一个选项,这个也就不难理解,就好比foot要不要到FOO的缓存中去找.Default: false

* multiple (Boolean):

是否允许输入多个值即多次使用autoComplete以输入多个值.Default: false

* multipleSeparator (String):

如果是多选时,用来分开各个选择的字符. Default:","

* scroll (Boolean):

当结果集大于默认高度时是否使用卷轴显示 Default: true

* scrollHeight (Number):

自动完成提示的卷轴高度用像素大小表示 Default: 180 

* formatItem (Function):

为每个要显示的项目使用高级标签.即对结果中的每一行都会调用这个函数,返回值将用LI元素包含显示在下拉列表中.Autocomplete会提供三个参数(row, i, max): 返回的结果数组, row:当前处理的行数(即第几个项目,是从1开始的自然数), max:当前结果数组元素的个数即项目的总个数. Default: none, 表示不指定自定义的处理函数,这样下拉列表中的每一行只包含一个值.

* formatResult (Function):

和formatItem类似,但可以将将要输入到input文本框内的值进行格式化.同样有三个参数,和formatItem一样.Default:none,表示要么是只有数据,要么是使用formatItem提供的值.

* formatMatch (Function):

对每一行数据使用此函数格式化需要查询的数据格式. 返回值是给内部搜索算法使用的. 参数值row

* extraParams (Object):

为后台(一般是服务端的脚本)提供更多的参数.和通常的作法一样是使用一个键值对对象.如果传过去的值是{ bar:4 },将会被autocompleter解析成my_autocomplete_backend.aspx?q=foo&bar=4 (假设当前用户输入了foo). Default: {}

* result (handler) Returns: jQuery

此事件会在用户选中某一项后触发,参数为:

event: 事件对象.event.type为result.

formatted:formatResult函数返回的值

$("#singleBirdRemote").result(function(event,data, formatted) {

//如选择后给其他控件赋值,触发别的事件等等

});

 

注意问题】:

在使用远程地址时,它默认传入的参数是:q(输入值),limit(返回结果的最大值),可以使用extraParams传入其他的参数。

 

--------------------------------------------------------------------------------------------------

笔者前后台结合的代码案例:

后台Java代码如下:

@RequestMapping(value ="queryautojson.do")

@ResponseBody

public void queryJqueryPluginJson(HttpServletRequestrequest, HttpServletResponse response) throws IOException {

        String jsondata =request.getSession().getServletContext().getAttribute("autoJson").toString();

       response.setContentType("text/plain; charset=UTF-8");

        response.setCharacterEncoding("UTF-8");

 

       response.setHeader("Pragma", "No-cache");

       response.setHeader("Cache-Control", "no-cache");

       response.setDateHeader("Expires", 0);

 

       response.getWriter().println(jsondata);

}

 

前台代码如下:

jQuery(document).ready(function(){

$.getJSON("mytest/manage/queryautojson.do",function(data){

               $('#keyword').autocomplete(data, {

                   max: 8, //列表里的条目数

                   minChars: 0,  //自动完成激活之前填入的最小字符

                   width: 153,   //提示的宽度,溢出隐藏

                   scrollHeight: 200, //提示的高度,溢出显示滚动条

                   matchCase:false,  //是否区分大小写

                   matchContains: false, //包含匹配,就是data参数里的数据,是否只要包含文本框里的数据就显示

                   autoFill: false, //自动填充

                   formatItem: function(row, i, max) {

                       return row.UNIT;

                   },

                   formatMatch: function(row, i, max) {

                       return row.UNIT;

                   },

                   formatResult: function(row) {

                       return row.ID;

                   }

               }).result(function(event, row, formatted) {

                          $("#content").val(row.NAME);

                       });

            });

 

        });

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值