当用户点击进入时,如何防止清除文本字段

时间:2013-05-30 19:19:31

标签: javascript jquery twitter-bootstrap

目前看来,我不是在bootstrap typeahead上自动选择(参见下面的代码),这工作正常,但是之前当用户点击输入时,它使用autoselect [0],现在它正在清除细胞,我该怎么做才能使它保持当前细胞的内容。

禁用自动选择并在bootstrap-typeahead.js上突出显示的代码:

$.fn.typeahead.Constructor.prototype.render = function (items) {
 var that = this;
 items = $(items).map(function (i, item) {
 i = $(that.options.item).attr('data-value', item);
 i.find('a').html(that.highlighter(item));
 return i[0];
});

this.$menu.html(items);
 return this;
};

http://jsfiddle.net/isherwood/vvK3b/

1 个答案:

答案 0 :(得分:2)

您已停止将类active放在突出显示的项目上,但bootstrap不知道如何处理此问题。您需要做的是更改select函数,如果没有active类,则不要执行它通常执行的操作。这是代码

$.fn.typeahead.Constructor.prototype.select = function () {
    if (this.$menu.find('.active').length) {
      var val = this.$menu.find('.active').attr('data-value')
      this.$element
        .val(this.updater(val))
        .change()
      return this.hide()
    }
}

http://jsfiddle.net/isherwood/vvK3b/1/

相关问题