Multiple array add in autocomplete

人走茶凉 提交于 2020-04-18 05:44:30

问题


I want to add multiple array in autocomplete. I have added aTags but how can I add 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


回答1:


You can use the Array.prototype.concat() function to achieve this.

You would need to change the source: aTags to 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>


来源:https://stackoverflow.com/questions/46604593/multiple-array-add-in-autocomplete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!