【问题标题】:Multiple array add in autocomplete多个数组添加自动完成
【发布时间】:2017-10-06 20:58:23
【问题描述】:

我想在自动完成中添加多个数组。我已经添加了aTags,但是如何添加bTags

var aTags = ["ask", "always", "all", "alright", "one", "foo", "blackberry", "tweet", "force9", "westerners", "sport"];

var bTags = ["aaaaaaa", "bbbbbbbb", "ccccccc", "ddddddddd"];


$("#tags").autocomplete({
  source: aTags,
  response: function(e, result) {
    if (!result.content.length) {
      console.log('No matches!');
      jQuery('#messag').html("Not match...").show();
    } else {
      jQuery('#messag').hide();
    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" />
<input type='text' title='Tags' id='tags' />
<span id="messag"></span>

Jsfiddle demo

【问题讨论】:

    标签: jquery arrays autocomplete


    【解决方案1】:

    您可以使用Array.prototype.concat() 函数来实现这一点。

    您需要将source: aTags 更改为source: aTags.concat(bTags)

    var aTags = ["ask", "always", "all", "alright", "one", "foo", "blackberry", "tweet", "force9", "westerners", "sport"];
    
    var bTags = ["aaaaaaa", "bbbbbbbb", "ccccccc", "ddddddddd"];
    
    
    $("#tags").autocomplete({
      source: aTags.concat(bTags),
      response: function(e, result) {
        if (!result.content.length) {
          console.log('No matches!');
          jQuery('#messag').html("Not match...").show();
        } else {
          jQuery('#messag').hide();
        }
      }
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" />
    <input type='text' title='Tags' id='tags' />
    <span id="messag"></span>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-29
      • 1970-01-01
      • 2020-12-24
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多