假设我想向原始的 bootstrap-typeahead.js 添加一些功能。
我不喜欢 fork 另一个库然后编辑它以添加功能或使用 fork 库的想法。
一个用例可能是:将选项 onselect 添加到 $.fn.typeahead.defaults
以便在我选择自动完成中的项目时执行回调。
这是我的测试(1),请添加注释和代码以使其正常运行。
(1)
_.extend($.fn.typeahead.defaults, {onselect: null});
element.typeahead({
minLength: 3,
source: function () {
return ['val1', 'val2'];
},
onselect: function (val) {
console.log(val); // it should be able to get the value selected
}
});
最佳答案
JavaScript 是一种非常动态的语言,允许您在运行时轻松地重新定义或修改函数。参见 this answer of mine举个例子。
但是,似乎有人已经 answered your question通过链接到 fork 的 typeahead 以提供 onselect
回调。
关于javascript - 如何扩展像 bootstrap-typeahead.js 这样的库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12120463/