解决方法
扩展typeahead类:
var newusertext = 'New user'; var newuserlink = '/add/user/0'; $.fn.typeahead.Constructor.prototype.process = function (items) { var that = this items = $.grep(items,function (item) { return that.matcher(item) }) items = this.sorter(items) if (!items.length) { return this.rendernonmatch().show() } return this.render(items.slice(0,this.options.items)).show() } $.fn.typeahead.Constructor.prototype.rendernonmatch = function () { var that = this var items = new Array(); items.push(newusertext) items = $(items).map(function (i,item) { i = $(that.options.item).attr('data-value',item) i.find('a').html(that.Highlighter(item)) return i[0] }) items.first().addClass('active') this.$menu.html(items) return this } /* redirect to the registration page to add a new user */ $.fn.typeahead.Constructor.prototype.updater = function (item) { if(item === newusertext){ window.location = newuserlink; return} return item }