jQuery autocomplete @mention

女生的网名这么多〃 提交于 2019-12-19 11:49:28

问题


I have this autocomplete plugin from Andrew Whitaker - DEMO Lets say I have a string in a textarea

"@peterwateber welcome"

I want it to output in a hidden tag as

"@[peterwateber] welcome"

how should I do it? I'm not that good at Javascript...

I've tried looking at this code here from Hawkee


回答1:


I wrote the original code mentioned here and have fixed the menu problem Peter was having:

http://www.hawkee.com/snippet/9391/




回答2:


Hiya Working demo here: http://jsfiddle.net/67dxH/

Already had good discussion above like you said behavior is like: value of the hidden tag as = @[C#] and the textarea as @C#

Jope this is helpful man, let me know how it goes, cheers! :)

Jquery Code

function split(val) {
    return val.split(/@\s*/);
}

function extractLast(term) {
    return split(term).pop();
}

function getTags(term, callback) {
    $.ajax({
        url: "http://api.stackoverflow.com/1.1/tags",
        data: {
            filter: term,
            pagesize: 5
        },
        type: "POST",
        success: callback,
        jsonp: "jsonp",
        dataType: "jsonp"
    });    
}

$(document).ready(function() {

    $("#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({
        source: function(request, response) {
            if (request.term.indexOf("@") >= 0) {
                $("#loading").show();
                getTags(extractLast(request.term), function(data) {
                    response($.map(data.tags, function(el) {
                        return {
                            value: el.name,
                            count: el.count
                        }
                    }));
                    $("#loading").hide();                    
                });
            }
        },
        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
            ui.item.value = "@" + ui.item.value;   
            terms.push(ui.item.value);
            // add placeholder to get the comma-and-space at the end
            terms.push("");
            this.value = terms.join("");
            return false;
        }
    }).data("autocomplete")._renderItem = function(ul, item) {
        return $("<li>")
            .data("item.autocomplete", item)
            .append("<a>@[" + item.label + "]&nbsp;<span class='count'>(" + item.count + ")</span></a>")
            .appendTo(ul);
    };
});​


来源:https://stackoverflow.com/questions/10060536/jquery-autocomplete-mention

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