2011-11-09 14 views
1

我有一個輸入文本框,用戶在其中輸入一些數據並按下Enter鍵。然後顯示一些建議,然後用戶應該能夠使用箭頭鍵從這些建議中選擇值。然後他使用回車鍵從建議中選擇一個數據,這個選定的值應該顯示在輸入文本字段中。 我可以使用鼠標執行所有操作,但現在我想使用箭頭鍵執行此操作。這是我如何實現使用jquery輸入關鍵事件。使用箭頭鍵從ajax建議文本框中選擇數據

$('#dishes').keydown(function (e){ 
    if(e.keyCode == 13){ 
     menusearch('dishes','<?=$user_id?>') ; 
    } 
}) 

我在看這些內容。

+0

這取決於'menusearch'函數生成的html。你是否可以將此添加到問題中? –

+0

@Layoric抱歉不能理解你的問題 – Sibu

+0

你的問題還不清楚'menusearch'函數在做什麼,這使得很難提供建議或幫助。如果你可以提供'menusearch'的代碼,我可能會提供一些建議。 –

回答

0

我有類似的任務,因爲你現在需要:

$("#searchterm2").keyup(function(event) { 
if (event.keyCode != '13' && event.keyCode != '38' && event.keyCode != '40') { //user input some symbol 
    if(helpbox_closed == 1){ //if autocomplete is still hidden 
     $('div.saveSearchTmp > input:last').val($("#searchterm2").val()); // saving user's value 
     $("#helpBox").show() //show autocomplete box 
        .load("searchAjax.php", {words: $("#searchterm2").val()},function(){ 
      checkAJAX(); //creating ajax request to get fields by user's value and setting helpbox_closed = 0 
     }); 
    } 
} 

if((event.keyCode == '40' || event.keyCode == '38') && $("#helpBox").css('display') == 'none') 
    $("#searchterm2").keyup(); //if user push up or down and autocomplete box is hidden we show it 
else if(event.keyCode == '40'){ 
    helpbox_closed = 0; 
    //your code here to move current li/div/or what you have 
}else if(event.keyCode == '38'){ 
    helpbox_closed = 0; 
    //your code here to move current li/div/or what you have 
}}); 

對不起,我沒有看到你的自動完成框HTML,所以這是所有我可以告訴你,你提供的信息。