我正在爲我的公司的內部Web表單工作我試圖使用typehead.js從本地數組中加載名稱。我可以毫無問題地完成此任務,但是第二部分任務是在第一個員工名稱被選中時,將該員工的ID放在第二個文本框中。我無法成功將該值輸出到文本框中。有誰知道這可以實現嗎?有typeahead.js輸出到多個文本框
我不能分享實際的代碼,因爲它是一個內部應用程序的一部分,但基本上它看起來像你的基本typehead.js形式。
<form>
<input type="text" class="typeahead" id="employee_name"/>
<input type="text" class="typeahead" id="employee_id"/>
</form>
<script>
employees.initialize(); //initialize the employee search
// instantiate the typeahead UI
$('#employee_name.typeahead').typeahead({
highlight: true
},
{
name: 'name',
displayKey: 'name',
source: employees.ttAdapter()
});
var employees = new Bloodhound({ //List of employees
datumTokenizer: function(d) { return Bloodhound.tokenizers.whitespace(d.name); },
queryTokenizer: Bloodhound.tokenizers.whitespace,
local: [
{ name: 'Employee 1', id: '12345' },
{ name: 'Employee 2', id: '54321' }
]
});
</script>
僅供參考,我還提出了有關[GitHub](https://github.com/twitter/typeahead.js)上typeahead('val',...)行爲的問題/問題/ 646)。開發者同意目前的行爲並不理想。 –
這樣做,謝謝! –
很高興我能幫助@RenzoGaspary :) –