Pra*_*bhu 11 javascript jquery autocomplete jquery-plugins jquery-autocomplete
我正在使用jquery UI AutoComplete来允许用户使用@mentions标记朋友.默认情况下,只要您将焦点放在文本框上,就会显示自动填充建议.只有在输入"@"时,如何才能显示建议?这是我到目前为止的代码:
var availableTags = [
"ActionScript",
"AppleScript",
"Asp",
"BASIC",
];
$("#tags").autocomplete({
source: availableTags,
minLength: 0
});
Run Code Online (Sandbox Code Playgroud)
And*_*ker 20
您可以通过为source自动完成选项提供一个功能来完成此操作:
var availableTags = [ /* Snip */];
function split(val) {
return val.split(/@\s*/);
}
function extractLast(term) {
return split(term).pop();
}
$("#tags")
// don't navigate away from the field on tab when selecting an item
.bind("keydown", function(event) {
if (event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) {
event.preventDefault();
}
}).autocomplete({
minLength: 0,
source: function(request, response) {
var term = request.term,
results = [];
/* If the user typed an "@": */
if (term.indexOf("@") >= 0) {
term = extractLast(request.term);
/* If they've typed anything after the "@": */
if (term.length > 0) {
results = $.ui.autocomplete.filter(
availableTags, term);
/* Otherwise, tell them to start typing! */
} else {
results = ['Start typing...'];
}
}
/* Call the callback with the results: */
response(results);
},
focus: function() {
// prevent value inserted on focus
return false;
},
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;
}
});
Run Code Online (Sandbox Code Playgroud)
这是一个有效的例子:http://jsfiddle.net/BfAtM/2/
请注意,这几乎等同于这个演示,除了为用户键入的要求"@".该代码位于sourceoption参数内.
希望有所帮助.
| 归档时间: |
|
| 查看次数: |
6017 次 |
| 最近记录: |