我使用的是带有以下代码的jQuery.textcomplete插件:
$('#textarea3').textcomplete([
{ // html
mentions: {
'test': 'test@gmail.com',
'othertest': 'othertest@gmail.com'
},
match: /B@(w*)$/,
search: function (term, callback) {
callback($.map(this.mentions, function (mention) {
return mention.indexOf(term) === 0 ? mention : null;
}));
},
index: 1,
replace: function (mention) {
return '@' + mention + ' ';
}
}
]);
当用户键入@te时,我想显示选项,但只显示mention.[value]
,我不想像一样返回replace: function
replace: function (mention) {
return '@' + mention.[key] + ' ';
}
那么,我该怎么办呢?
将提到dict的内容转换为数组,您就可以完成。
var mentions = {
'test': 'test@gmail.com',
'othertest': 'othertest@gmail.com'
};
var arrayOfMentions = Object.keys(mentions).map(function(key) {
var val = {};
val[key] = mentions[key];
return val;
});
$('textarea').textcomplete([
{
mentions: arrayOfMentions,
match: /B@(w*)$/,
search: function (term, callback) {
callback($.map(this.mentions, function (mention) {
var value = mention[Object.keys(mention)[0]];
return value.indexOf(term) === 0 ? mention : null;
}));
},
template: function (mention) {
return mention[Object.keys(mention)[0]];
},
index: 1,
replace: function (mention) {
var key = Object.keys(mention)[0];
return '@' + key + ' ';
}
}
]);
请参阅jsbinhttp://jsbin.com/farewa/edit?js,输出
类似的东西。。。我在拉小提琴。。。
callback($.map(this.mentions, function (mention) {
for (property in this.mentions) {
return mention.indexOf(property) === 0 ? mention : null;
}
}));
我的情况与此相反:我需要在keys
中搜索,但替换为value
:
$('#maintext').textcomplete([{
mentions: arrayOfMentions,
match: /B@(w*)$/,
search: function(term, callback) {
callback($.map(this.mentions, function(mention) {
var key = Object.keys(mention)[0];
return key.indexOf(term) === 0 ? mention : null;
}));
},
template: function(mention) {
return '@' + Object.keys(mention)[0];
},
index: 1,
replace: function(mention) {
var key = Object.keys(mention)[0];
if(mention[key]){
return mention[key];
} else {
return '@' + key + ' ';
}
}
}]);
请参见此处:https://jsfiddle.net/FabioBeneditto/od5jumvh/