从ajax建议文本框中使用箭头键选择数据

时间:2011-11-09 07:20:55

标签: php javascript jquery

我有一个输入文本框,用户输入一些数据并按下回车键。然后显示一些建议,然后用户应该能够使用箭头键从这些建议中选择值。然后,他使用回车键从建议中选择数据,此选定值应显示在输入文本字段中。    我可以使用鼠标执行所有这些,但现在我想使用箭头键执行此操作。这就是我使用jquery实现enter key事件的方法。

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

我正在寻找这些方面的东西。

1 个答案:

答案 0 :(得分: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,所以这就是我可以告诉您的所有信息。