0
我在我的html中使用Typeahead。我想顯示區域名稱並獲取區域ID。但我都顯示並獲得區域ID。如何解決它?Javascript - 在Typeahead中顯示名稱並獲取id
MYFILE,JS:
.
.
.
var regions = response.data.result;
console.log(regions);
我想表明我的預輸入regionName並獲得regionId。
var regionValue = regions.map(function(result) {
return result.regionId;
});
$scope.autocomplete = $('.the-basics .typeahead').typeahead({
hint: false,
highlight: false,
minLength: 1,
datumTokenizer: true
},{
name: 'states',
source: substringMatcher(regionValue),
templates:{
empty:[
'<div class="empty-message"> No result...</div>'
]
}
})
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);
};
};
其實我想顯示regionName並得到regionId。任何建議?