具有唯一值的组合框中的 Jquery 自动完成功能

Jquery auto complete feature in combo box with unique values

本文关键字:功能 成功 Jquery 唯一 组合      更新时间:2023-09-26

我在组合框中使用 Jquery ui 自动完成插件,我正在从 JSON 文件中读取值。问题出在我的 JSON 文件中。我有具有相同值的字段。喜欢这个。({名称:A},{名称:A},{名称:B})

因此,当我在组合框中键入"a"时,它会给我 2 个"a"。但我只需要一个(我只需要 JSON 文件中的唯一值)。我该怎么做?我现在没有完整的代码,这就是为什么我不能放它。对此感到抱歉,谢谢。

编辑:在将数据发送到jQuery自动完成插件之前,您可以使用这样的东西从json数组中删除重复的条目。

var names = {};
var param = "name"
$.each(data.people, function() {
    if (!names[this[param]])
       names[this[param]] = [];   
    names[this[param]].push(this);
});

然后我们可以做source: names

试试这个....只能在输入字段中添加唯一值

 select: function( event, ui ) {
      var terms = split( this.value );
      // remove the current input
      terms.pop();
      // add the selected item
      if(!($.inArray(ui.item.value,terms) > -1))
      terms.push( ui.item.value );

      // add placeholder to get the comma-and-space at the end
      terms.push( "" );
      this.value = terms.join( ", " );
      return false;
    }