Bootstrap typeahead为隐藏字段添加值
Bootstrap typeahead add value to hidden field
我在用户数据库中使用了typeahead,一切似乎都很好。然而,我正在执行的搜索是一个成员的姓氏。
我想做的是,一旦用户被点击,我想让它将用户ID添加到字段中,而不是使用他们在字段中输入的姓氏。
有办法做到这一点吗?
$('input.query').typeahead({
name: 'query',
value: 'lastName',
remote: 'jsonUser.php?query=%QUERY',
minLength: 3,
template: ["<div class='typeahead_wrapper'>",
"<img class='typeahead_photo' src='http://localhost/iwebimage.axd?qid={{qid}}'/>",
"<div class='typeahead_labels'>",
"<div class='typeahead_primary'>{{firstName}} {{lastName}} <small>({{ntid}})</small></div>",
"<div class='typeahead_secondary'>{{department}}</div>",
"</div>",
"</div>", ].join(''),
engine: Hogan,
limit: 8,
valueKey: 'lastName'
});
更新:
JSON响应包含用户的ID,只是不确定如何让插件使用另一个值作为最终值,而不是您正在搜索的值。
这就是对他有效的方法:
$('input.query').on('typeahead:selected', function (e, datum) {
console.log(datum['empID']);
});
我还发现Bootstrap Typeahead更新程序不起作用,它建议使用相同的
注意:twitter反对bootstrap/tyhead,并建议您使用https://github.com/twitter/typeahead.js带有引导程序3
将valueKey: 'empID'
添加到您的预编型参数中,这将是您从预编型中选择项目后放入搜索框的值
编辑:bootstrap 3不再支持更新程序。这个答案只适用于引导程序2预编型
覆盖更新程序函数。并将CCD_ 2添加到输入
$('input.query').typeahead({
updater: function(item) {
//item the json object that the user selected
return item.id; // set input's value to item.id
},
//....
)};
Html
<input class="query" data-provide="typeahead" />
相关文章:
- JS动态添加字段-删除按钮不起作用
- 向laravel表单动态添加字段
- MaskMoney.js无法处理动态添加字段
- 流星:在创建帐户上添加字段
- 使用DB值动态添加字段到Form,php
- 使用object.freeze()扩展函数对象-can't添加字段
- 在ASP.Net web窗体/MVC中动态加载和添加字段
- 如何在使用Knex.JS添加字段时捕捉错误
- KendoUI 网格:动态向数据源添加字段
- Jquery- 动态添加字段 - 防止删除最后一个项目
- JQuery 无法获取动态添加字段的值
- 扩展 Ext.data.Model(动态添加字段)
- 如何使用角度 JS 添加嵌套的添加字段
- 如何在 CKeditor 上传中向 POST 值添加字段
- 在流星中向用户集合添加字段的正确方式
- 按数组分组并在主数组中添加字段和子数组
- 如何在JavaScript中为HTML上的动态添加字段执行乘法
- 如何在javascript中为html上的动态添加字段匹配id
- 在具有嵌套属性的轨道中动态添加字段
- 使用jquery添加字段