多个数组添加自动完成功能

时间:2017-10-06 11:21:01

标签: jquery arrays autocomplete

我想在自动完成中添加多个数组。我添加了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>
&#13;
&#13;
&#13;

Jsfiddle demo

1 个答案:

答案 0 :(得分:1)

您可以使用Array.prototype.concat()功能来实现此目的。

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

&#13;
&#13;
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();
    }
  }
});
&#13;
<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>
&#13;
&#13;
&#13;