我正在嘗試創建一個自動提示框。當用戶開始在#tagSelection輸入字段中輸入內容時,JQuery通過Ajax從數據庫中獲取建議,並將這些建議顯示在右側#tagSelection字段下顯示的#suggestBox div中。當放開鍵時刪除JQuery keydown事件更改
現在我想讓用戶使用向下箭頭鍵選擇其中一個建議。我已經開始通過處理keydown()事件並在此事件期間爲第一個條目分配一個類來構建它。我面臨的問題是,當我放開箭頭鍵時,班級會再次被移除。當我放開鑰匙時,我需要保留它。那可能嗎?
的HTML
<div class="form-entry">
<input id="tagSelection" name="tags" type="text" value="" size="40">
<div id="suggestBox" style="">
<a href="#" id="1">design</a>
<a href="#" id="3">debit card</a>
<a href="#" id="4">deer</a>
<a href="#" id="addTag">Add word</a>
<div id="selectedTags"></div>
</div>
jQuery的
(function() {
$('#tagSelection').keydown(downArrowKeyHandler);
})();
function downArrowKeyHandler(event) {
if (event.keyCode == 40) {
if ($('#suggestBox').length > 0) {
if ($('[tagSelected = 1]').length == 0) {
// the tagSelected class gives the entry a colored background
$('#suggestBox').children().filter(':first').addClass('tagSelected');
}
}
}
}
40是向下鍵ASCII碼? –
什麼是''[tagSelected = 1]'?你的意思是「.tagSelected」嗎? (或者也許你正在使用一種我不知道的技術。)上面的代碼看起來並不像它可能刪除類;在別處檢查其他事件處理鉤子。 –