2015-03-03 29 views
0

我使用的是Twitter Typeahead,並且在將鼠標懸停在觸發懸停建議的建議彈出到我的輸入字段時遇到一些問題。如果用戶然後按下輸入,實際的盤旋值將被選擇而不是用戶寫入的值。我希望僅在用戶點擊實際建議時才能選擇該選項。 這似乎是,當鼠標懸停的建議被解僱的如何覆蓋onSuggestionMouseEnter for Twitter typeahead

_onSuggestionMouseEnter() 

功能。我的問題是,如果有可能在我自己的代碼中覆蓋這個函數?或以另一種方式解決這個問題?

這是我的預輸入代碼:

var suggestions = new Bloodhound({ 
    datumTokenizer: function(d) { 
     return Bloodhound.tokenizers.whitespace(d.value); 
    }, 
    queryTokenizer: Bloodhound.tokenizers.whitespace, 
    limit: 100, 
    remote: { 
     url: 'url/to/suggestions', 
     filter: function(list) { 
      return $.map(list.suggestions, function(word) { 
       return {s: word.title}; 
      }); 
     } 
    } 
}); 

suggestions.initialize(); 
$('.typeahead').typeahead({highlight: true}, { 
    source: suggestions.ttAdapter(), 
    name: 'suggestions', 
    displayKey: 's', 
    hint: false 
}) 

回答

0

以供將來參考我解決它通過增加對這樣輸入按鍵檢查:

$("#search-input").keydown(function (e) { 
    if (e.keyCode == 13 && hoverSuggestion) { 
    $('.typeahead').typeahead('val', $('.twitter-typeahead pre').text()); 
    $('#search-input').text($('.twitter-typeahead pre').text()) 
    $('#search-form').submit(); 
    } 
}); 

的hoveSuggestion變量用於檢查箭頭已經被使用過,如果沒有,那麼就不要去清理這些建議。它是這樣變化的:

hoverSuggestion = true; 
$('.typeahead').typeahead({highlight: true}, { 
    source: suggestions.ttAdapter(), 
    name: 'suggestions', 
    displayKey: 's', 
    hint: true 
}).on('typeahead:cursorchanged', function(event) { 
    hoverSuggestion = false; 
}); 

不是最優雅的解決方案,但它現在的作品。反饋非常歡迎!