Javascript 使用jQuery文本完成一个包含键和值的数组
我正在使用带有以下代码的插件:Javascript 使用jQuery文本完成一个包含键和值的数组,javascript,jquery,arrays,Javascript,Jquery,Arrays,我正在使用带有以下代码的插件: $('#textarea3').textcomplete([ { // html mentions: { 'test': 'test@gmail.com', 'othertest': 'othertest@gmail.com' }, match: /\B@(\w*)$/, search: function (term, callback) {
$('#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时显示选项,但只显示提及。[值]
并且我不想返回替换:函数
replace: function (mention) {
return '@' + mention.[key] + ' ';
}
那么,我该怎么办呢?像这样的事情。。。我在做小提琴
callback($.map(this.mentions, function (mention) {
for (property in this.mentions) {
return mention.indexOf(property) === 0 ? mention : null;
}
}));
将提及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 + ' ';
}
}
]);
请参见jsbin处的我的情况与之相反:我需要搜索键
,但替换为值
:
$('#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 + ' ';
}
}
}]);
请看这里:其他人的回答对你有用吗?如果没有,我会继续。上面的链接引用了一个断开的textcomplete链接。这是一个更新的垃圾箱: