`
仅此而已
  • 浏览: 59414 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

jquery自动提示控件autocomplete使用总结

阅读更多
引入必要的js文件一共三个
jquery.js
<script type='text/javascript' src='${jsdir}/autocomplete/jquery.autocomplete.js'></script>
<link href="${jsdir}/autocomplete/jquery.autocomplete.css" rel="stylesheet" type="text/css" />

$(function(){
				var temp;
				$.ajax({
				   type: "POST",
				   url: "suggest.action",
				   async: false,//锁定浏览器为 temp赋值


				   success: function(data){
				   			temp=eval(data);//将字符串转换为数组对象赋值给temp


				   }
				}); 
				$("#startperson").autocomplete(temp, {
					matchContains: true,
					minChars: 0
				});
			})
====================================
struts2中action代码
public class SuggestAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private static final Logger LOG = Logger.getLogger(SuggestAction.class);
	private String suggestMessage;//返回的提示信息
	private List<GClientinfo> clientinfoList;
	
	public String execute() throws Exception {
		clientinfoList = manager.query(new StringBuffer("select new GClientinfo(keyid,clientname) from GClientinfo ").toString());//查询获得list数据
		StringBuffer suggestestMessageBuffer = new StringBuffer();
		if (clientinfoList==null||clientinfoList.isEmpty()) {
			return StrutsResultType.NONE;
		}
		for (GClientinfo clientinfo : clientinfoList) {
			if (suggestestMessageBuffer.length()==0) {
				suggestestMessageBuffer.append("[\"")
														  .append(clientinfo.getClientname())
														  .append('\"');
				continue;
			}
			suggestestMessageBuffer.append(",\"")
													  .append(clientinfo.getClientname())
													  .append('\"');;
		}
		suggestestMessageBuffer.append("]");
		HttpServletResponse response = ServletActionContext.getResponse(); 
		response.setCharacterEncoding("UTF-8");
		PrintWriter out= response.getWriter();
		out.print(suggestestMessageBuffer.toString());//生成的格式为"["sdfsdf","wer","werwer","werwe","werw"]"out输出给页面


	    return StrutsResultType.NONE;
	}

//省略get、set方法
}

================================== 
困扰我半天的问题居然是浏览器不兼容一下内容转自  lichdb 


jQuery.Autocomplete 是jquery的流行插件,能够很好的实现输入框的自动完成(autocomplete)、建议提示(input suggest)功能,支持ajax数据加载。

但唯一遗憾的是,在对中文输入法打开时,firefox3.0中是对中文拼音的自动匹配,而对输入后的中文无法及时触发匹配;而在我的IE6.0下,则无此问题。

原因分析:
Autocomplete插件对用户输入字符的触发自动匹配是通过”keydown”事件进行的(可分析 jquery.autocomplete.js第92行),在IE6中,当输入法打开时,输入的字符是不会触发”keydown”的,只有中文输入完毕才 触发之,所以中文输入和latin文没有区别的;但在firefox3.0下,无论输入法打开否,按键都会触发”keydown”事件,所以造成中文输入 完毕,自动匹配的是刚才打出的部分中文拼音字母。

解决方法:
网上查到的最多做法是修改jquery.autocomplete.js第92行,将”keydown”替换为”keyup”, 但这个不是根本办法,虽然这样改后可在firefox中及时对输入的中文进行自动匹配,但将原插件中回车、tab等重要的事件机制破坏了,比如这样改后, 如果你的input是在一个form里的话,回车从原来的将选定项输入到input中变为了直接提交form表单了,这并不是我们想要的。

我的方法原理是,补充一个原插件触发查询的事件,就是当input输入栏发生字符变化时,重新进行查询(调用其内部的onChange函数),这里 主要针对firefox而言,因为我们的系统访问最多的是IE和firefox。而恰好firefox有一个input变化的事件就是oninput ,那么我们只要在原jquery.autocomplete.js第199行,插入如下代码:

  1. . bind ( " input " , function () {
  2.     // @hack by liqt:support for inputing  chinese characters  in firefox
  3.     onChange ( 0 , true ) ;
  4. }) ;
==========================================
再转一些配置信息感谢paskaa


对autocomplete的一些参数进行说明

Options:

Name Type minChars delay cacheLength matchSubset matchCase matchContains mustMatch selectFirst extraParams
Number Default: 1

执行autocomplete的最小值

The minimum number of characters a user has to type before the autocompleter activates.

Number Default: 400 for remote, 10 for local

显示自动自动完成选择框的延迟时间

The delay in milliseconds the autocompleter waits after a keystroke to activate itself.

Number Default: 10

缓存长度

The number of backend query results to store in cache. If set to 1 (the current result), no caching will happen. Must be >= 1.

Boolean Default: true

匹配子集

Whether or not the autocompleter can use a cache for more specific queries. This means that all matches of "foot" are a subset of all matches for "foo". Usually this is true, and using this options decreases server load and increases performance. Only useful with cacheLength settings bigger than one, like 10.

Boolean Default: false

是否敏感的比较。

Whether or not the comparison is case sensitive. Important only if you use caching.

Boolean Default: false

匹配内容。

Whether or not the comparison looks inside (i.e. does "ba" match "foo bar") the search results. Important only if you use caching. Don't mix with autofill.

Boolean Default: false

必须完全匹配。

If set to true, the autocompleter will only allow results that are presented by the backend. Note that illegal values result in an empty input box.

Boolean Default: true

如果设置为true则第一个值会被自动选中。

If this is set to true, the first autocomplete value will be automatically selected on tab/return, even if it has not been handpicked by keyboard or mouse action. If there is a handpicked (highlighted) result, that result will take precedence.

Object


 

今天添加个自动提示控件,一番周折。具体步骤:

0
0
分享到:
评论

相关推荐

    JQuery autocomplete Ajax分页控件

    JQuery autocomplete ajax分页,对源码稍微修改了下

    jquery autocomplete 完成控件中文修正

    jquery autocomplete.js 是一款优秀的开源JS,支持自动完成,不过如果是中文失效。原因是由于中文编码不一致引起的,修改源文件中AJAX请求中的编码,加上escape编码后,在后台用Server.URLDecode解码后,则中文也...

    jquery.autocomplete

    jquery.autocomplete的js+css 自动查询的显示的空控件,jquery.autocomplete.js和jquery.autocomplete.css文件

    jquery autocomplete 控件

    autocomplete autocomplete控件 例子 包括源文件

    jquery.autocomplete.js

    jquery.autocomplete.min.js文本框的自动填充控件,jquery解决了跨浏览器问题。

    封装好的Jquery 的ajax 的json的AutoComplete控件(用户控件)

    封装好的Jquery 的ajax 的json的AutoComplete控件(用户控件),json序列化也做好了,拎包入住

    jQuery UI AutoComplete 使用说明

    介绍 在 jQuery UI 的最近更新中增加了自动完成控件 AutoComplete,这为我们提供了又一个强大的开发工具,这里介绍一下这个控件的使用方式。 首先,jQuery UI 是基于 jQuery 的,所以,必须在你的页面中首先引用 ...

    jquery autocomplete控件,非常好用,提供源代码,有详细示例

    1、普通text input,通过$("#Text1").KingAutoSelect(settings, DropDownColumns, "/CommonPage/getUserList.ashx")即可控件化; 2、支持键盘上、下键选择,回车即可选中相应数据,其中“值“存放于控件的...

    jquery.autocomplete的js+css

    自动查询的显示的空控件,jquery.autocomplete.js和jquery.autocomplete.css文件

    autocomplete--jquery自动填充

    AutoComplete控件就是在用户在文本框输入前几个字母或是汉字的时候,该控件就能从存放数据的文本或是数据库里将所有以这些字母开头的数据提示给用户,供用户选择,提供方便。

    纯JQUERY自查询(完成)控件

    轻量级自查询(完成)控件,提供二次接口。具体说明请看博客文章: http://blog.csdn.net/u011616825/article/details/9792929

    jquery+jsp/html实现自动完成控件功能(有例子)

    使用jQuery+CSS 做了一个输入框自动完成的功能,代码不多,有例子,简单易懂。有HTML和JSP两个格式的。使用到了JSON数据格式。

    JQuery教程自学笔记总结

    3.3.2 JQuery UI Autocomplete(自动完成) 56 3.3.3 JQuery UI Button(按钮) 63 3.3.4 JQuery UI Datepicker (时间控件) 69 3.3.5 JQuery UI Dialog (对话框) 79 3.3.6 JQuery UI Menu (JQuery菜单) 88 ...

    jQM Autocomplete 1.5.2.zip

    jQM Autocomplete是一个自动提示相关内容的jQuery Mobile搜索插件。用户在搜索框输入前几个字母或是汉字的时候,Autocomplete就能从存放数据的文本或是数据库里将所有以这些字母开头的数据提示给用户,供用户选择,...

    仿百度自动搜索控件,自动搜索

    1. 基于Jquery 2. 基于jquery.autocomplete 3. net 4. 类似百度自动搜索功能 4. 如有问题可发邮件至我的邮箱:xiaohan_0305@hotmail.com

    Jquery+Html5实现自动补全(模糊查询)小例子.zip

    使用HTML5+Jquery实现Autocomplete功能,本资源中主要是举例控件如何使用。 $(document).ready(function(){ $("#autocomplete").autocomplete({ minLength:0, autoFocus:false, source: availableTags, ...

    jquery autocomplete自动完成插件的的使用方法

    首先下载所需文件,jquery.autocomplete.js和jquery.autocomplete.css。 由于该控件获得数据可以从数组和URL两种方式获取,所以写了两个简单的小例子试验一下。 前台代码如下: 代码如下: &lt;&#37;@ Page Language=...

    JQuery教程自学笔记

    3.3.2 JQuery UI Autocomplete(自动完成) 56 3.3.3 JQuery UI Button(按钮) 63 3.3.4 JQuery UI Datepicker (时间控件) 69 3.3.5 JQuery UI Dialog (对话框) 79 3.3.6 JQuery UI Menu (JQuery菜单) 88 3.3.7 ...

    基于jquery实现智能提示控件intellSeach.js

    例如:某公司人事管理系统,想搜索李XX,只要输入“李”,系统自然会提示一些姓李的员工,这样方便用户使用。说白了,就是用户边输入,系统会提示相关的结果;或者,当用户点击搜索框时,就推荐一些内容,如360、...

Global site tag (gtag.js) - Google Analytics