Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jQuery文本完成一个包含键和值的数组_Javascript_Jquery_Arrays - Fatal编程技术网

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链接。这是一个更新的垃圾箱: