Programmatically triggering typeahead.js result display

后端 未结 17 1892
余生分开走
余生分开走 2020-11-30 05:01

I am using Twitter\'s typeahead.js (https://github.com/twitter/typeahead.js/) on an input field which is pre filled from a query string. After loading the page, i\'d like to

17条回答
  •  生来不讨喜
    2020-11-30 05:07

    Looking through the source code, it appears to store a TypeaheadView object in the element data under the key typeahead. This TypeaheadView object has an internal method, _showDropdown, which is internally bound to the focus event (and a few others).

    I would not recommend doing this, but you should be able to call that internal method manually:

    $('#yourTypeaheadElement').data('typeahead')._showDropdown();
    

    Alternatively, have you just tried simply focusing your typeahead element when the page loads (after initializing it as a typeahead element, of course):

    // after page loads and yourTypeaheadElement is initialized as a typeahead
    $('#yourTypeaheadElement').focus();
    

提交回复
热议问题