How to prevent Twitter typeahead from filling in the input with the selected value?

前端 未结 5 1574
深忆病人
深忆病人 2020-12-18 08:17

I\'m using Twitter\'s typeahead plugin for autocompletion with a text input. I want to change the behavior so that instead of putting the selected option\'s value into the t

5条回答
  •  隐瞒了意图╮
    2020-12-18 08:48

    While not necessarily the intention of typeahead, if you use it for a SEARCH PAGE this little tidbit will be very useful I feel as I was looking for this answer myself.

    Unbind Selection Event From Typeahead Results List:

    $('.typeahead.users').typeahead({
                rateLimitWait: 250,
                limit: 100,
                valueKey: 'title'
    
    }).on('typeahead:opened', function (obj, datum, name) {
        $('span.tt-dropdown-menu').unbind(); //UNBINDS TYPEAHEAD SELECTION EVENT
    });
    

    WTF Did You Do? : -- When typeahead object is opened, we simply unbind the selection action altogether in order to do what we want it to instead without closing it.

    Further Notes: This is sort of destructive, but if you are using for, say, a search page where the body of the result item is link to a profile with a follow button on the right to click - then the auto-filling is a super pain in the ass to fight off.

    Hope this helps! Know its a bit late but maybe anyone else looking for this in the future.

提交回复
热议问题