开发者

AUTO-SUGGESTION keyboard use

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Ajax Auto Suggest</title>
        <script type="text/javascript" src="jquery-1.2.1.pack.js"></script>
        <script type="text/javascript">
        var stringcount = 0;
        var st = "";
        var vv = "f";
        function lookup2(e,inpstring)
        {
            lookup1(e.keyCode,inpstring);
        }
        function lookup1(j,inputstring)
        {
            var x= inputstring.length;
            st = inputstring ;
            if (inputstring.charAt(parseInt(x,10)-1) == " ")
            {
                stringcount = stringcount + 1;
            }
            else
            {
                var mySplitResult = inputstring.split(" ");
                var stringtemp = "" ;
                var w = 0;
                for (w =0 ; w < stringcount ;w++)
                {
                stringtemp = stringtemp+ " "+ mySplitResult[w];
                }
                st = stringtemp;
                lookup(mySplitResult[stringcount],inputstring);
            }
          }
            function lookup(inputString,i) {
                if(inputString.length == 0) {
                    // Hide the suggestion box.
                    $('#suggestions').hide();
                } else {
                    $.post("rpc.php", {queryString: ""+inputString+"" }, function(data){
                        if(data.length >0) {
                            $('#suggestions').show();
                            $('#autoSuggestionsList').html(data);
                        }
                    });
                }
            } // lookup

            function fill(thisValue) {
                $('#inputString').val(st.substring(1,st.length)+" "+thisValue);
                setTimeout("$('#suggestions').hide();", 200);
            }
    </script>
        <style type="text/css">
            body {
                font-family: Helvetica;
                font-size: 11px;
                color: #000;
            }
            h3 {
                margin: 0px;
                padding: 0px;
            }
            .suggestionsBox {
                position: relative;
                left: 30px;
                margin: 10px 0px 0px 0px;
                width: 200px;
                background-color: #212427;
                -moz-border-radius: 7px;
                -webkit-border-radius: 7px;
                border: 2px solid #000;
                color: #fff;
            }
            .suggestionList {
                margin: 0px;
                padding: 0px;
            }
            .suggestionList li {
                margin: 0px 0px 3px 0px;
                padding: 3px;
                cursor: pointer;
            }
            .suggestionList li:hover {
                background-color: #659CD8;
            }
        </style>
    </head>
    <body>
        <div>
            <form>
                <div>Type your county here:<br />
                    <input type="text" size="30" value="" id="inputString" onkeyup="lookup2(event,this.value);" onblur="" />
                </div>
                <div class="suggestionsBox" id="suggestions" style="display: none;">
                    <img src="upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
                    <div class="suggestionList" id="autoSuggestionsList">&nbsp;</div>
                </div>
            </form>
        </div>
    </body>
</html>

This is the code i am using. The auto-suggestion box is accessed by clicking on the desired option. How can i scroll throu开发者_开发知识库gh the option by using the up/down keys of the keyboard and select an option by using enter?


It looks like (because you have not quoted the really important code) that your server side ajax endpoint returns an HTML unordered list and this is pasted into the suggestionList div. That's going to be my assumption. Your CSS allows for the hover pseudo-selector so mouse support looks good.

For keyboard support, you are going to have add an event handler for the keypress event, probably on the document. Add the handler when the suggestion box is displayed, remove it when it is dismissed.

The event handler will have to track the up and down arrow keys as well as enter. You will have to add and remove a special class (or maybe an id) on the li element that is currently selected, which means you will have to track how many elements there are to scroll through, and which one is the currently highlighted one. So, if you see the down arrow key, add one to the current index (if you're at the last one, ignore the key). Remove the special class from the li element you just left and add it to the new one (obviously style the class accordingly in your CSS). When the enter key is pressed you know which element is selected, so return it, or do what you want with it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜