---
title: "autocomplete"
author: "giga_Zhang"
date: "2012-01-30T13:41:55Z"
tags: []
categories: ["Web前端"]
slug: "autocomplete"
draft: false
lastmod: "2012-01-30T18:57:19Z"
id: 1383261
url: "https://zjsoft.iteye.com/blog/1383261"
---

$("#department").autocomplete('/oms/orgAjax/getSimilarNames', {
	 	width: 162,
	 	minChars: 2,
        dataType: 'json',
        highlight: false,
        scroll: true,
        scrollHeight: 300,
        parse: function(data) {
			var array = new Array();
			for(var i = 0 ; i < data.orgNames.length; i++){
		        array = {data: data.orgNames*, value: data.orgNames*, result: data.orgNames*};
			}
			return array;
        },
        formatItem: function(row){
        	return row;
        },
        onReSelect: function(options) {
	        alert("reselect" + options);
	        return options;
        }
	}).result(function(event, item) {
		alert("result" + item);
	});;


自定义事件onReSelect，在源码中添加如下代码：
function onChange(crap, skipPrevCheck) {
		 if (options.onReSelect) {
			var op = options.onReSelect(options);
			if (op) {
				$.extend(options, op);
			}
		}
		
		if( lastKeyPressCode == KEY.DEL ) {
			select.hide();
			return;
		}
		
		var currentValue = $input.val();
		
		if ( !skipPrevCheck && currentValue == previousValue )
			return;
		
		previousValue = currentValue;
		
		currentValue = lastWord(currentValue);
		if ( currentValue.length >= options.minChars) {
			$input.addClass(options.loadingClass);
			if (!options.matchCase)
				currentValue = currentValue.toLowerCase();
			request(currentValue, receiveData, hideResultsNow);
		} else {
			stopLoading();
			select.hide();
		}
	};


http://jquery.sanchezsalvador.com/page/jquerycombobox.aspx