我有来自 Andrew Whitaker 的这个自动完成插件 - DEMO假设我有一个字符串textarea
“@peterwateber 欢迎”
我希望它在隐藏标签中输出为
“@[彼得瓦特伯] 欢迎”
我该怎么做?我不是很擅长 Javascript...
我试过 从 Hawkee看这段代码
Hiya在这里工作演示:http : //jsfiddle.net/67dxH/
已经在上面进行了很好的讨论,就像你说的行为是这样的:value of the hidden tag as = @[C#] and the textarea as @C#
乔佩这是个乐于助人的人,让我知道进展如何,干杯!:)
jQuery代码
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 + "] <span class='count'>(" + item.count + ")</span></a>")
.appendTo(ul);
};
});
我编写了此处提到的原始代码并修复了彼得遇到的菜单问题: