2017-10-06 29 views
0

我想在自動完成中添加多個數組。我添加了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

回答

1

可以使用Array.prototype.concat()功能來實現這一目標。

您將需要改變source: aTagssource: 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>