jquery autocomplete to start on pressing character '@'

无人久伴 提交于 2019-12-07 12:41:51

问题


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?


回答1:


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.




回答2:


$('#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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!