2016-05-08 11 views
0

私は自分のhtmlにTypeaheadを使用しています。私は地域名を表示し、地域IDを取得したい。私は地域IDを表示して取得しています。どのようにそれを解決するには?Javascript - Typeaheadで名前を表示してIDを取得する

のmyFile、JS: enter image description here

私は私の先行入力でregionNameを示し、regionIdを取得したい:

. 
. 
. 
var regions = response.data.result; 
console.log(regions); 

ここでの結果のスクリーンショットの要素を点検しています。

  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を取得したいと考えています。なにか提案を?

答えて

1

私が何をしたいことは、ドロップダウンregionNameに表示されていることがあることを理解し、あなたが項目を選択すると、あなたはあなたがこの

$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>' 
       ], 
       suggestion: function(data) {  
         return '<p> + data.regionName + '</p>'; 
        } 
      } 
     }) 

を行うregionNameのみ表示するには、次の何ができる場合は、IDを取得し、あなたはdocs typeahead

でこの

$('.the-basics .typeahead').bind('typeahead:selected', function(obj,datum, name) { 
    //put here you code after selected item 
    datum.regionId 
}) 

ルックをしますかの項目を選択したときに要素のidを取得します

関連する問題