I have a textarea in which I write messages. I want to activate jquery autocomplete for certain condition like - when user presses '@', they should start seeing the suggestion till they dont press space.
It is something similar to facebook style @friends prediction.
Does jquery inherently support this? Any ideas?
I think the easiest way to do this is to provide a function to source. Something like this:
textbox.autocomplete({
source: function (request, response) {
var lastWord = request.term.split(/,\s*/).pop();
if (lastWord.indexOf('@') == -1) {
return [];
}
$.getJSON('YourUrl' + lastWord, {}, function (data) {
response(values);
});
}
});
This will only work if the last part is the one being autocompleted though.
$('#target').keydown(function(event) {
if (event.keyCode == 64) {
//do something for @
}
if (event.keyCode == 32) {
//do something for space
}
});
you can see ajax samples in google -> "jquery ajax"
来源:https://stackoverflow.com/questions/6705999/jquery-autocomplete-to-start-on-pressing-character