jQuery UI Autocomplete常用的参数有:
其他不常用的就不罗列了。
假如页面上有以下输入框:
<input type="text" id="autocomp" />
通过指定source为服务器端的地址来实现,如下:
1
2
3
4
|
$(
"#autocomp"
).autocomplete({
source:
"remote.ashx"
,
minLength: 2
});
|
然后在服务器端接收,并输出相应结果,注意默认传递的参数名称为term:
public void ProcessRequest(HttpContext context) { // 查询的参数名称默认为term string query = context.Request.QueryString["term"]; context.Response.ContentType = "text/javascript"; //输出字符串数组 或者 JSON 数组 context.Response.Write("[{\"label\":\"博客园\",\"value\":\"cnblogs\"},{\"label\":\"囧月\",\"value\":\"囧月\"}]"); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
// 本地字符串数组
var
availableTags = [
"C#"
,
"C++"
,
"Java"
,
"JavaScript"
,
"ASP"
,
"ASP.NET"
,
"JSP"
,
"PHP"
,
"Python"
,
"Ruby"
];
$(
"#local1"
).autocomplete({
source: availableTags
});
// 本地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
});
|
通过指定source为自定义函数来实现自定义数据的获取,函数主要有2个参数(request,response),分别用于获取输入的值、呈现结果
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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);
//呈现结果
}
});
|
直接从官方DEMO拿来的,通过发送ajax请求到远程服务器,然后对返回结果进行处理,最后通过response来呈现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
$(
"#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
});
|
jQuery UI Autocomplete有一些事件,可用于在一些阶段进行额外的控制:
这些事件的ui参数的item属性(如果有的话)默认有label和value属性,不管在source中设置的数据是Array还是JSON数组,如下3种:
1
2
3
|
[
"cnblogs"
,
"博客园"
,
"囧月"
]
[{label:
"博客园"
, value:
"cnblogs"
}, {label:
"囧月"
, value:
"囧月"
}]
[{label:
"博客园"
, value:
"cnblogs"
, id:
"1"
}, {label:
"囧月"
, value:
"囧月"
, id:
"2"
}]
|
假如是第三种的话,还可以得到ui.item.id的值。
这些事件可以通过2种方式来绑定,如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
// 在参数中
$(
"#autocomp"
).autocomplete({
source: availableTags
, select:
function
(e, ui) {
alert(ui.item.value)
}
});
// 通过bind来绑定
$(
"#autocomp"
).bind(
"autocompleteselect"
,
function
(e, ui) {
alert(ui.item.value);
});
|
通过bind来绑定的方式使用的事件名称为"autocomplete"+事件名称,如"select"就是"autocompleteselect"。
一般情况下,输入框的autocomplete仅需要一个值就可以(如:javascript);假如需要多个值(如:javascript,c#,asp.net),则需要绑定一些事件来进行额外处理:
还是直接拿官方DEMO的代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
// 按逗号分隔多个值
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);
}
}));
|