我想要一个带自动填充的搜索框,因此会将我重定向到该页面。如果我输入“Au”,它会显示我澳大利亚并将打开该页面。这就是问题。我使用过typehead.js,但我不能使用链接。我的意思是我应该把链接放在哪里?
请参阅以下代码以供参考:
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substringRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
matches.push(str);
}
});
cb(matches);
};
};
var states = ['Afghanistan', 'Albania', 'Antigua and Barbuda', 'Argentina', 'Armenia',
'Australia', 'Austria', 'Vatican City', 'Venezuela', 'Vietnam', 'Yemen', 'Zambia',
'Zimbabwe'];
$(document).ready(function(e) {
$('#the-basics .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
}, {
name: 'states',
source: substringMatcher(states)
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/twitter/typeahead.js/588440f6/dist/typeahead.jquery.min.js"></script>
<div id="the-basics">
<input class="typeahead" type="text" placeholder="Type the name of the country">
</div>