Keyup或Keydown事件时的Typeahead Fill Text Box

时间:2016-06-29 06:59:43

标签: jquery typeahead.js

我正在尝试将建议的typeahead字段列表(.typeahead)中的值添加到另一个文本输入中。例如,当我输入“A”并根据建议值执行keyup或keydown时,值应显示在文本字段.result中如何在typeahead中执行此操作。有什么特别的方法可以挂钩吗?

<input type="text" class="typeahead box"/>
<input type="text" class="result" placeholder="Result"/>
<script>
var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
  'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
  'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
  'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
  'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
  'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
  'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
  'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
  'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];

var states = new Bloodhound({
  datumTokenizer: Bloodhound.tokenizers.whitespace,
  queryTokenizer: Bloodhound.tokenizers.whitespace,
  // `states` is an array of state names defined in "The Basics"
  local: states
});

$('.typeahead').typeahead({
  hint: true,
  highlight: true,
  minLength: 1
},
{
  name: 'states',
  source: states
});

</script>

1 个答案:

答案 0 :(得分:0)

我已经找到了自己。使用typeahead:cursorchange获取值。

$('.typeahead').bind('typeahead:cursorchange ', function(ev, suggestion) {
  $('.result').val(suggestion); 
});