Programmatically triggering typeahead.js result display

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

    I used this to manually trigger a selection, when 1 item was in the tt-dataset. It just made sense to add this.

    $("#mytypeahead").keyup(function (e) {
                    var charCode = (typeof e.which === "number") ? e.which : e.keyCode;
                    if (charCode == 13) {
                        if ($(this).parent().find(".tt-dataset").children().length == 1) {
                            //This is how we are submitting a single selection on enter key
                            $(this).parent().find(".tt-dataset").children(0).addClass("tt-cursor");
                            var kde = jQuery.Event("keydown");
                            kde.which = 13;
                            $(this).trigger(kde);                           
                        }    
                    }                    
                });
    

提交回复
热议问题