<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Fancy Validate - jQuery UI Autocomplete</title>
<link href="jquery-ui/css/ui-lightness/jquery-ui-1.8.17.custom.css" rel="stylesheet" />
<script src="jquery-1.7.1.min.js"></script>
<script src="jquery-ui/js/jquery-ui-1.8.17.custom.min.js"></script>
<script src="jquery-ui/js/jquery-ui-widget-combobox.js"></script>
<style>
body {
font-size: 14px;
}
fieldset {
width: 388px;
margin: 0 auto;
text-align: right;
}
fieldset div {
margin: 15px auto;
}
.cbo .ui-button-text {
line-height: 1.3;
padding-top: 0;
padding-bottom: 0;
}
.cbo .ui-autocomplete-input {
width: 7.2em;
}
</style>
<script>
$(function() {
/* ajax autocomplete */
// ajax 返回字符串数组
$("#ajax1").autocomplete({
source: "remoteArray.ashx",
minLength: 2
});
// ajax 返回json数组
$("#ajax2").autocomplete({
source: "remoteJSON.ashx",
minLength: 2
});
/* local source autocomplete */
// 本地字符串数组
var availableTags = [
"C#",
"C++",
"Java",
"JavaScript",
"ASP",
"ASP.NET",
"JSP",
"PHP",
"Python",
"Ruby"
];
$("#local1").autocomplete({
source: availableTags
/*, select: function(e, ui) {
alert(ui.item.value)
}*/
});
/*$("#local1").bind("autocompleteselect", function(e, ui) {
alert(ui.item.value);
});*/
// 本地json数组
var availableTagsJSON = [
{ label: "C# Language", value: "C#" },
{ label: "C++ Language", value: "C++" },
{ label: "Java Language", value: "Java" },
{ label: "JavaScript Language", value: "JavaScript" },
{ label: "ASP.NET", value: "ASP.NET" },
{ label: "JSP", value: "JSP" },
{ label: "PHP", value: "PHP" },
{ label: "Python", value: "Python" },
{ label: "Ruby", value: "Ruby" }
];
$("#local2").autocomplete({
source: availableTagsJSON
});
/* custom source autocomplete */
// 自定义source函数
var hosts = ["gmail.com", "live.com", "hotmail.com", "yahoo.com", "cnblogs.com", "火星.com", "囧月.com"];
$("#email1").autocomplete({
autoFocus: true,
source: function(request, response) {
var term = request.term, //request.term为输入的字符串
ix = term.indexOf("@"),
name = term, // 用户名
host = "", // 域名
result = []; // 结果
result.push(term);
// result.push({ label: term, value: term }); // json格式
if (ix > -1) {
name = term.slice(0, ix);
host = term.slice(ix + 1);
}
if (name) {
var findedHosts = (host ? $.grep(hosts, function(value) {
return value.indexOf(host) > -1;
}) : hosts),
findedResults = $.map(findedHosts, function(value) {
return name + "@" + value; //返回字符串格式
// return { label: name + " @ " + value, value: name + "@" + value }; // json格式
});
result = result.concat($.makeArray(findedResults));
}
response(result); //呈现结果
}
});
/* jsonp autocomplete */
// 自定义source函数
$("#jsonp").autocomplete({
source: function(request, response) {
$.ajax({
url: "http://ws.geonames.org/searchJSON",
dataType: "jsonp",
data: {
featureClass: "P",
style: "full",
maxRows: 12,
name_startsWith: request.term
},
success: function(data) {
response($.map(data.geonames, function(item) {
return {
label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
value: item.name
}
}));
}
});
},
minLength: 2
/*, select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.label :
"Nothing selected, input was " + this.value);
},*/
});
/* multi value autocomplete */
// 按逗号分隔多个值
function split(val) {
return val.split(/,\s*/);
}
// 提取输入的最后一个值
function extractLast(term) {
return split(term).pop();
}
// 按Tab键时,取消为输入框设置value
function keyDown(event) {
if (event.keyCode === $.ui.keyCode.TAB &&
$(this).data("autocomplete").menu.active) {
event.preventDefault();
}
}
var options = {
// 获得焦点
focus: function() {
// prevent value inserted on focus
return false;
},
// 从autocomplete弹出菜单选择一个值时,加到输入框最后,并以逗号分隔
select: function(event, ui) {
var terms = split(this.value);
// remove the current input
terms.pop();
// add the selected item
terms.push(ui.item.value);
// add placeholder to get the comma-and-space at the end
terms.push("");
this.value = terms.join(", ");
return false;
}
};
// 多个值,本地数组
$("#local3").bind("keydown", keyDown)
.autocomplete($.extend(options, {
minLength: 2,
source: function(request, response) {
// delegate back to autocomplete, but extract the last term
response($.ui.autocomplete.filter(
availableTags, extractLast(request.term)));
}
}));
// 多个值,ajax返回json
$("#ajax3").bind("keydown", keyDown)
.autocomplete($.extend(options, {
minLength: 2,
source: function(request, response) {
$.getJSON("remoteJSON.ashx", {
term: extractLast(request.term)
}, response);
}
}));
/* combobox autocomplete */
$("#combo1").combobox();
});
</script>
</head>
<body>
<form ac