`
78425665
  • 浏览: 125383 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

使jQuery autocomplete支持中文搜索

 
阅读更多
附件里是修改过的 jquery.autocomplete.js(其实只是escape,unescape下输入框值)

页面代码:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<script type="text/javascript" src="js/jquery-1.5.1.min.js"></script>
<script type="text/javascript" src="js/jquery.autocomplete.js"></script>
<link rel="stylesheet" href="style/jquery.autocomplete.css" type="text/css" media="screen, projection"/>
<script type="text/javascript">
$(function(){
	$("#txtSubSysInfo").autocomplete("/case/task/getSubSysInfo.shtml", {
		minChars: 0,
		width: 400,
		mustMatch: true,
		autoFill: false,
		multiple: true,
		multipleSeparator: "; ",
		parse: function(data){
			var parsed = [];
			var rows = data;
			for (var i=0; i < rows.length; i++) {
				var row = rows[i];
				if (row) {
					parsed[parsed.length] = {
						data: row,
						value: row.ID,
						result: this.formatResult(row)
					};
				}
			}
			return parsed;
		},
		formatItem: function(row, i, max) {
			return "[" + row.ID + "]" + row.ENNAME + "(" + row.CNNAME + ")";
		},
		formatResult: function(row) {
			return row.CNNAME;
		}
	}).result(function(e, data, value){
		$('#hidSubSysInfo').val(value);
	});


});
</script>
</head>
<body>

<input style="width: 600px;" id="txtSubSysInfo" name="txtSubSysInfo"/></br>
<input style="width: 600px;" id="hidSubSysInfo" name="hidSubSysInfo"/>
</body>
</html>


后台代码:
	public  ModelAndView  getSubSysInfo(HttpServletRequest request ,HttpServletResponse response) throws Exception{
		String queryString = request.getParameter("q");
		Writer out = null;
		try {	
			response.setContentType("text/json; charset=gbk");
			out = response.getWriter();
			queryString = queryString == null ? "": queryString;
			queryString = DateUtil.unescape(DateUtil.unescape(queryString));
			int index = queryString.lastIndexOf(";")+1;
			queryString = queryString.substring(index == -1 ? 0 : index).trim();
			
			List<Map> sysList = this.serviceInstance.getSubSysInfo(queryString.toUpperCase());
			
			String sysJson = JSONArray.fromObject(sysList).toString();
			out.write(sysJson);
		} catch (Exception e) {
			log.error("获取子系统异常", e);
			JSONObject jsonObject = new JSONObject();
			jsonObject.element("errorMsg", e.getMessage());
			out.write(JSONArray.fromObject(jsonObject).toString());
		}finally{
			out.flush();
			out.close();
		}
		return null;
	}


后台把解密了两次,其解密的方法是:
	private final static String[] hex = { "00", "01", "02", "03", "04", "05",
			"06", "07", "08", "09", "0A", "0B", "0C", "0D", "0E", "0F", "10",
			"11", "12", "13", "14", "15", "16", "17", "18", "19", "1A", "1B",
			"1C", "1D", "1E", "1F", "20", "21", "22", "23", "24", "25", "26",
			"27", "28", "29", "2A", "2B", "2C", "2D", "2E", "2F",

			"30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3A",
			"3B", "3C", "3D", "3E", "3F", "40", "41", "42", "43", "44", "45",
			"46", "47", "48", "49", "4A", "4B", "4C", "4D", "4E", "4F", "50",
			"51", "52", "53", "54", "55", "56", "57", "58", "59", "5A", "5B",
			"5C", "5D", "5E", "5F",

			"60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6A",
			"6B", "6C", "6D", "6E", "6F", "70", "71", "72", "73", "74", "75",
			"76", "77", "78", "79", "7A", "7B", "7C", "7D", "7E", "7F", "80",
			"81", "82", "83", "84", "85", "86", "87", "88", "89", "8A", "8B",
			"8C", "8D", "8E", "8F", "90", "91", "92", "93", "94", "95", "96",
			"97", "98", "99", "9A", "9B", "9C", "9D", "9E", "9F",

			"A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8", "A9", "AA",
			"AB", "AC", "AD", "AE", "AF", "B0", "B1", "B2", "B3", "B4", "B5",
			"B6", "B7", "B8", "B9", "BA", "BB", "BC", "BD", "BE", "BF", "C0",
			"C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CA", "CB",
			"CC", "CD", "CE", "CF", "D0", "D1", "D2", "D3", "D4", "D5", "D6",
			"D7", "D8", "D9", "DA", "DB", "DC", "DD", "DE", "DF", "E0", "E1",
			"E2", "E3", "E4", "E5", "E6", "E7", "E8", "E9", "EA", "EB", "EC",
			"ED", "EE", "EF", "F0", "F1", "F2", "F3", "F4", "F5", "F6", "F7",
			"F8", "F9", "FA", "FB", "FC", "FD", "FE", "FF" };

	private final static byte[] val = { 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
			0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
			0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
			0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
			0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x00, 0x01,
			0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x3F, 0x3F, 0x3F,
			0x3F, 0x3F, 0x3F, 0x3F, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x3F,
			0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
			0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
			0x3F, 0x3F, 0x3F, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x3F, 0x3F,
			0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
			0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
			0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
			0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
			0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
			0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
			0x3F, 0x3F, 0x3F, 0x3F, 0x3F,

			0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
			0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
			0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
			0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
			0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
			0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
			0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F,
			0x3F, 0x3F, 0x3F };

	public static String escape(String s) {
		StringBuffer sbuf = new StringBuffer();
		int len = s.length();
		for (int i = 0; i < len; i++) {
			int ch = s.charAt(i);
			if (ch == ' ') { // space : map to '+'
				sbuf.append('+');
			} else if ('A' <= ch && ch <= 'Z') { // 'A'..'Z' : as it was
				sbuf.append((char) ch);
			} else if ('a' <= ch && ch <= 'z') { // 'a'..'z' : as it was

				sbuf.append((char) ch);
			} else if ('0' <= ch && ch <= '9') { // '0'..'9' : as it was
				sbuf.append((char) ch);
			} else if (ch == '-'
					|| ch == '_' // unreserved : as it was
					|| ch == '@'
					|| ch == '.' || ch == '!' || ch == '~' || ch == '*'
					|| ch == '\'' || ch == '(' || ch == ')') {
				sbuf.append((char) ch);
			} else if (ch <= 0x007F) { // other ASCII : map to %XX
				sbuf.append('%');
				sbuf.append(hex[ch]);
			} else { // unicode : map to %uXXXX

				sbuf.append('%');
				sbuf.append('u');
				sbuf.append(hex[(ch >>> 8)]);
				sbuf.append(hex[(0x00FF & ch)]);
			}
		}
		return sbuf.toString();
	}

	public static String unescape(String s) {
		StringBuffer sbuf = new StringBuffer();
		int i = 0;
		int len = s.length();

		while (i < len) {
			int ch = s.charAt(i);
			if (ch == '+') { // + : map to ' '
				sbuf.append('+');
			} else 
			if ('A' <= ch && ch <= 'Z') { // 'A'..'Z' : as it was
				sbuf.append((char) ch);
			} else if ('a' <= ch && ch <= 'z') { // 'a'..'z' : as it was
				sbuf.append((char) ch);
			} else if ('0' <= ch && ch <= '9') { // '0'..'9' : as it was
				sbuf.append((char) ch);
			} else if (ch == '-'
					|| ch == '_' // unreserved : as it was
					|| ch == '.' || ch == '!'
					|| ch == '@'
					|| ch == '~' || ch == '*' || ch == '\'' || ch == '/' || ch == '('
					|| ch == ')') {
				sbuf.append((char) ch);
			} else if (ch == '%') {
				int cint = 0;
				if ('u' != s.charAt(i + 1)) { // %XX : map to ascii(XX)
					cint = (cint << 4) | val[s.charAt(i + 1)];

					cint = (cint << 4) | val[s.charAt(i + 2)];
					i += 2;
				} else { // %uXXXX : map to unicode(XXXX)
					cint = (cint << 4) | val[s.charAt(i + 2)];

					cint = (cint << 4) | val[s.charAt(i + 3)];
					cint = (cint << 4) | val[s.charAt(i + 4)];
					cint = (cint << 4) | val[s.charAt(i + 5)];
					i += 5;
				}
				sbuf.append((char) cint);
			}

			i++;
		}
		return sbuf.toString();
	}
分享到:
评论

相关推荐

    java实现jquery.autocomplete自动搜索含中文

    用java实现autocomplete搜索功能,可以获取除输入框的其它动态参数,后台字符串拼接,返回json数据,格式如下: { query:'Li', suggestions:['Liberia', 'Libyan Arab Jamahiriya', 'Liechtenstein', 'Lithuania'...

    利用Jquery.AutoComplete插件及微软提供的ChineseConverter.dll实现汉字、拼音、简拼模糊搜索

    本教程将详细介绍如何结合Jquery.AutoComplete插件和微软的ChineseConverter.dll库,实现在网页上进行汉字、拼音和简拼的模糊搜索功能。 首先,Jquery.AutoComplete是jQuery UI库中的一个组件,它能为输入框添加...

    PHP jquery autocomplete实现

    jQuery Autocomplete默认支持上下键移动,用户可以通过上/下箭头键选择提示列表中的项目。如果需要自定义行为,可以通过监听`keydown`事件进行扩展。 ```javascript $("#searchInput").on('keydown', function...

    jquery.autocomplete 增加自定义查询方法

    总之,通过自定义`jquery.autocomplete`的查询源函数,我们可以实现更复杂、更个性化的查询策略,比如支持拼音首字母查询和多关键字查询,从而提高用户体验。结合提供的CSS和HTML文件,我们可以构建出美观且功能强大...

    Jquery AutoComplete firefox 中文 Ajax (option url or data) Jquery rails 自动完成

    **jQuery AutoComplete** 是一个非常流行的JavaScript库,用于在输入框中实现自动补全功能,它极大地提高了用户体验,尤其在需要用户输入关键字进行搜索或筛选的场景中。这个功能是基于jQuery框架构建的,因此可以...

    jquery自动完成autocomplete,兼容各浏览器,支持中文

    本文将深入探讨jQuery Autocomplete的实现原理、兼容性、中文支持以及如何与JSON数据源结合使用。 ### 1. 实现原理 jQuery Autocomplete主要通过监听用户在输入框中的输入事件,实时发送AJAX请求到服务器获取匹配...

    jQuery.Autocomplete 中文支持.docx

    《jQuery.Autocomplete 中文支持详解》 jQuery.Autocomplete 是一个广受欢迎的 jQuery 插件,它提供了输入框的自动完成和建议提示功能,极大地增强了用户体验。该插件允许通过 AJAX 方式动态加载数据,使得实时搜索...

    jquery搜索自动提示下拉框插件autocomplete-有我自己做的例子-支持中文

    在使用 jQuery Autocomplete 实现中文搜索时,我们需要确保数据源(通常是JSON格式)包含了正确的编码,通常为UTF-8。同时,要确保HTML页面的meta标签也指定了正确的字符集,例如`&lt;meta charset="UTF-8"&gt;`。这样,...

    jquery autocomplete combobox

    在标题提到的问题中,“jquery-easyui的combobox火狐下不支持中文匹配”,这是一个常见的问题,因为原生的EasyUI Combobox可能并未充分考虑跨浏览器的兼容性,特别是在处理中文字符的搜索和匹配上。Firefox和其他...

    jquery.autocomplete

    ### 支持中文的关键点 1. **字符编码**:由于中文字符的特殊性,确保服务器和客户端之间的字符编码一致非常重要。通常需要设置UTF-8编码以正确处理中文。 2. **数据格式化**:Autocomplete的数据源可以是JSON格式...

    jquery.autocomplete.js 自动补全

    1. **中文支持**:autocomplete.js 支持中文字符,无论是简体还是繁体,都能进行有效的补全,满足中文环境下的应用需求。 2. **中间字查询**:不同于传统的首字母匹配,该插件支持用户输入任意中间字进行查询,提高...

    Jquery-autocomplete,自动提示,源代码,中文。

    **解决中文显示问题** 是jQuery Autocomplete的一个关键特性。由于JavaScript默认编码问题,中文字符在某些情况下可能无法正确显示。本压缩包中的源代码已经解决了这个问题,确保在自动提示列表中,中文可以正确无误...

    autocomplete插件附Demo(支持中文搜索)

    通常,我们需要对数据源进行编码处理,使其支持中文搜索。 例如,如果数据源是从服务器异步获取的,需要确保服务器返回的JSON数据正确编码,如使用UTF-8编码。同时,前端处理数据时也要保持相同的编码规则。 ```...

    jquery.autocomplete的使用示例及说明文档

    1. **中文匹配**:确保你的数据源支持中文字符,同时在设置 `source` 时处理好中文编码问题。 2. **性能优化**:对于大量数据,考虑使用异步加载或缓存策略,以避免性能瓶颈。 3. **自定义样式**:可以通过 CSS 调整...

    autocomplete汉字与拼音的双重查询

    本项目"autocomplete汉字与拼音的双重查询"特别关注了中文环境下的应用,不仅支持基于汉字的自动补全,还同时考虑了拼音的匹配,使得用户在输入汉字或拼音时都能得到准确的建议。 首先,我们要理解的是`...

    一个基于jQuery的autocomplete(gb2312、utf-8)

    本篇文章将深入探讨一个基于jQuery的Autocomplete插件,它支持GB2312和UTF-8编码,对于中文环境有着良好的适应性。 jQuery Autocomplete插件是jQuery UI库中的一个重要组件,它为文本输入框提供了自动完成的功能。...

    自动补全 autocomplete 例子 支持中文 数据库

    对于支持中文,我们需要处理的是汉字字符集,这与英文字符处理有所不同。在C#或PHP中,可以使用Unicode编码来处理中文字符,确保程序能够正确识别和处理中文。 在C#中,可以利用`System.Windows.Forms....

    JQuery 自动显示 模糊查询

    - **中文支持**:插件可以处理中文字符,适合中文检索场景。 - **自定义数据源**:可以绑定到数组、JSON 数据、AJAX 请求等多种数据源。 - **多种样式和布局**:可以通过 CSS 自定义显示样式,适应不同设计需求。 - ...

    JQuery UI 中文帮助文档

    - **AUTOCOMPLETE(自动补全)**: 在输入框中提供动态的建议列表,提高输入效率,常用于搜索框和表单输入。 ### 2. 使用jQuery UI的步骤 1. **引入库文件**:首先在HTML文件中引入jQuery库和jQuery UI的CSS与JS...

Global site tag (gtag.js) - Google Analytics