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:

NameType
minChars Number Default: 1

执行autocomplete的最小值

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

delay Number Default: 400 for remote, 10 for local

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

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

cacheLength 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.

matchSubset 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.

matchCase Boolean Default: false

是否敏感的比较。

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

matchContains 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.

mustMatch 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.

selectFirst 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.

extraParams Object


 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
jQuery Autocomplete 是一个 jQuery 插件,用于实现输入框自动完成功能。它可以根据用户输入的文本,从预设的数据源中提供匹配项,并在下拉列表中展示这些项供用户选择。 下面是使用 jQuery Autocomplete 的详细步骤: 1. 引入 jQueryjQuery Autocomplete 插件的 JS 文件。可以从 jQueryjQuery Autocomplete 的官方网站上下载相关文件。 ```html <script src="jquery.min.js"></script> <script src="jquery.autocomplete.min.js"></script> ``` 2. 在 HTML 中添加一个输入框,并为其设置一个 ID。 ```html <input type="text" id="autocomplete-input"> ``` 3. 在 JavaScript 中初始化 Autocomplete 插件。需要指定输入框的 ID、数据源和其他选项。 ```javascript $('#autocomplete-input').autocomplete({ lookup: ['Apple', 'Banana', 'Cherry', 'Durian', 'Elderberry'], minChars: 1, onSelect: function (suggestion) { alert('You selected: ' + suggestion.value); } }); ``` 其中,`lookup` 属性指定了数据源,可以是一个数组或一个函数。`minChars` 属性指定了最小匹配字符数,当用户输入的字符数小于该值时,不会触发自动完成。`onSelect` 属性指定了用户选择某个匹配项后的回调函数。 4. 根据需要自定义 Autocomplete 的样式。可以使用 CSS 修改下拉列表的样式,或修改 Autocomplete 插件的默认选项。 ```css .autocomplete-suggestions { border: 1px solid #999; background: #FFF; overflow: auto; } .autocomplete-suggestion { padding: 2px 5px; white-space: nowrap; cursor: pointer; } .autocomplete-suggestion:hover { background: #F0F0F0; } .autocomplete-group { font-weight: bold; } ``` 以上就是使用 jQuery Autocomplete 实现输入框自动完成的详细步骤。可以根据实际需求,自定义 Autocomplete 的选项和样式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值