Programmatically triggering typeahead.js result display

后端 未结 17 1882
余生分开走
余生分开走 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:06

    According to my tests (see fiddle), the focus() method is necessary in order to display the dropdown. So:

    theVal = $('.typeahead').val();
    $(".typeahead").typeahead('val', '')
    $(".typeahead").focus().typeahead('val',theVal).focus();
    
    • On line 1, we're assigning the current value of the Typeahead input to the variable theVal;
    • On line 2 we simply reset typeahead's computed value; and
    • On line 3 we're putting back the original value and the focus, which results in the suggestion dropdown displaying as if the user had typed something.

    I actually needed this to strongly encourage users to select from the Bloodhound suggestions coming from a geocoding API call so I could ensure the coordinates were obtained by the client prior to submitting a form.

提交回复
热议问题