【问题标题】:Twitter's Bootstrap TypeaheadTwitter 的 Bootstrap Typeahead
【发布时间】:2013-08-13 23:39:53
【问题描述】:

当我从淘汰视图模型函数填充搜索框时,我试图触发 Typeahead。

这是一个演示我的问题的 js Fiddle。当我点击提交按钮时,搜索字段会填充“apples”这个词,但不会触发预先输入。

    <div>
    <input id="btnSearch" data-bind='value: search'>
    <br>
    <button id="btnSubmit">Send String to Search Box From Here</div>

    function ViewModel() {
    this.search = ko.observable("");
    this.call = function () {
        this.search('apples');
    }
}

vm = new ViewModel();

var data = ["apples", "bananas", "peaches"];
$("#btnSearch").typeahead({
    source: data
});

$("#btnSubmit").click(function () {
    vm.call();
});
ko.applyBindings(vm);

http://jsfiddle.net/c8nps/4/

【问题讨论】:

    标签: jquery twitter-bootstrap knockout.js


    【解决方案1】:

    用 typeahead 代替 val() 触发 typeahead

    $('#searchVendor').typeahead('setQuery', 'value-you-want-to-set');
    

    或者更好

    jQuery#typeahead('setQuery');
    

    我会在您的代码中将其解释为 -

    $#searchVendor(myValue()); || $#searchVendor('mike'); 
    

    这是一个 GitHub 的问题 https://github.com/twitter/typeahead.js/issues/247

    【讨论】:

    • 我确实遇到过这个问题,昨天尝试过,然后意识到它使用 typeahead.js,不幸的是我在这里使用的是'bootstrap twitter typeahead',这种方法似乎不起作用。
    • 您知道 Bootstrap 被称为 Twitter Bootstrap 并且使用 jQuery 的 typeahead 对吗? getbootstrap.com/2.3.2/customize.html#plugins
    • 如果它不适合你考虑创建一个小提琴,我可以告诉你如何从 JavaScript 中做到这一点。
    • 我现在明白了,是的,我很困惑。让我试一试,看看会发生什么。非常感谢。
    • 我试过 $('#searchVendor').typeahead('setQuery','Mike');我收到一条消息,说明对象没有方法“setQuery”。我看到了下面的链接,第二条评论让我更加困惑。 github.com/twitter/typeahead.js/issues/361
    猜你喜欢
    • 2016-04-03
    • 1970-01-01
    • 2013-07-20
    • 2012-03-03
    • 2013-02-04
    • 2013-05-18
    • 2014-05-23
    • 2013-01-27
    • 1970-01-01
    相关资源
    最近更新 更多