twitter-bootstrap 如何更新 bootstrap-typeahead.js 中的源选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13516396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to update the source Option in bootstrap-typeahead.js
提问by Lorraine Bernard
I am using bootstrap-typeahead in order to allow multiple selection. Here is the demo.
我正在使用 bootstrap-typeahead 以允许多选。这是演示。
The original code has been update by @Sherbrow Twitter bootstrap typeahead multiple values
原始代码已由@Sherbrow Twitter bootstraptypeahead multiple values更新
My question is related to the following use case:
after inserting Alaskavalue, I would like to update the source not showing again Alaskavalue.
Any hints?
我的问题与以下用例有关:
插入Alaska值后,我想更新源不再显示Alaska值。
任何提示?
回答by toxicate20
I had the same problem and this one will save you a lot of time. I've updated your old jsFiddlewith my code example. The basic thing is that you need to do
我遇到了同样的问题,这个问题会为您节省很多时间。我已经用我的代码示例更新了你的旧jsFiddle。基本的事情是你需要做的
var autocomplete = $('input').typeahead();
autocomplete.data('typeahead').source = newSource;
Where newSourceis the new array. Now you just need a function that adds or removes an element, or whatever you need to do with it.
newSource新阵列在哪里。现在您只需要一个添加或删除元素的函数,或者您需要对其进行的任何操作。
回答by micahblu
None of the given answers worked for me, I had to destroy the original typeahead instance and re initialize it.
给定的答案都不适用于我,我不得不销毁原始的预输入实例并重新初始化它。
$('input').typeahead('destroy').typeahead(options);
$('input').typeahead('destroy').typeahead(options);
回答by Sherbrow
Based on the default updatermethod of typeahead :
基于updatertypeahead的默认方法:
updater: function (item) {
var pos = this.source.indexOf(item);
if(pos != -1) {
var newSource =
this.source.slice(0,pos)
.concat(this.source.slice(pos+1));
this.source = newSource;
}
return item
}
Demo with multiple values (jsfiddle)
Keep in mind that you can access this source from anywherewith $('sel').data('typeahead').sourceconsidering that the typeahead is initialized
请记住,你可以访问该源的任何地方与$('sel').data('typeahead').source考虑到预输入初始化

