【问题标题】:Change source at runtime for autocomplete在运行时更改源以自动完成
【发布时间】:2011-08-03 21:20:37
【问题描述】:

我计划为多个来源实现 jquery-ui-autocomplete。这是我的场景:我想根据某些条件拥有多个自动完成源。这是我的代码:

var arrLookup = ['a', 'b', 'c', 'd'];
var arrA = ['a1', 'a2', 'a3', 'a4'];
var arrB = ['b1', 'b2', 'b3', 'b4'];
var arrC = ['c1', 'c2', 'c3', 'c4'];
var arrD = ['d1', 'd2', 'd3', 'd4'];

$('#txt').autocomplete({
   source: function (request, response) {
         // delegate back to autocomplete, but extract the last term
         response($.ui.autocomplete.filter(arrLookup, request.term.split(/,\s*/).pop()));
   },
   minLength: 1,
   select: function (event, ui) {
      var terms = split(this.value);
      // remove the current input
      terms.pop();
      // add the selected item
      terms.push(ui.item.value);
      // add placeholder to get the comma-and-space at the end
      this.value = terms.join(", ");
      return false;
   }
});

现在我想要的是,最初自动完成源应该是 arrLookup。如果我选择 a,如果我选择 b,则源应修改为 arrA;他源修改为arrB,依此类推。现在因为它将是一个多值自动完成,一旦我点击“,”源切换回 arrLookup。谁能帮我切换来源。我知道只能通过源来实现,但我无法获得正确的条件。

【问题讨论】:

  • 我有点困惑。难道你不能把逻辑放在你的 source 函数中以查看正确的数组吗?
  • 我试过了,但我无法获得这样做的逻辑。首先是我需要提取最后一个“,”之后的所有数据。然后我需要再次解析数据以切换数据源。我觉得有点复杂。虽然可行,但无法得到正确的条件。任何帮助将不胜感激。

标签: jquery autocomplete jquery-ui-autocomplete


【解决方案1】:

您可能想尝试使用 jQuery 自动完成插件(它有一个“setOptions”方法)而不是 jQuery-ui 插件。

查看此 post 了解更多信息。

更新: Here 是另一个例子,说明如何在仍然使用 jQuery-ui 的同时做到这一点。看看这个人最初发布的最后一个帖子。

【讨论】:

    猜你喜欢
    • 2013-08-28
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 2011-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多