Pat*_*ard 5 jquery typeahead bootstrap-typeahead typeahead.js
我目前正在为我的网站使用twitter typeahead插件,如此处所示.
列表打开,在选择时,隐藏的输入字段将使用选择ID进行更新.我需要typeahead强制选择,以便ID与用户输入的内容一起使用.如果值更改,则应删除id,直到进行有效选择.如果盒子没有选择就关闭,他们都应该被清空.
到目前为止,我有:
$('.cp').typeahead({
name: 'nom',
prefetch: 'ajax_recherche_ville.php',
limit: 50
});
$('.cp').bind('typeahead:selected', function(obj, datum) {
$('input:hidden[name=ville_id]').val(datum.id);
});
Run Code Online (Sandbox Code Playgroud)
我找不到适用于此代码的强制选择示例 - 特别是因为我使用了预取方法.因此,我无法验证值是否在列表中,或者至少我不知道如何.如何让它按预期运行?
我不知道有什么直接的解决方案,但我想我可以提供一个解决方法。
您可以清除事件上的隐藏输入,稍后change()将在事件上更新:typeahead:selected
$('.cp').typeahead({
name: 'nom',
prefetch: 'ajax_recherche_ville.php',
limit: 50
}).change(function () {
$('input[name=ville_id]').val(''); // don't use :hidden since it slow down performance and doesn't do anything special, I suppose the input with name ville_id is unique
}).bind('typeahead:selected', function(obj, datum) {
$('input[name=ville_id]').val(datum.id);
});
Run Code Online (Sandbox Code Playgroud)
该方法的弱点是:
typeahead:selected)才能填充隐藏的输入演示: http: //jsfiddle.net/hieuh25/zz85q/
希望能帮助到你。