Navigate through list with up/down arrows

走远了吗. 提交于 2020-01-06 14:02:04

问题


Find the JSFiddle here: http://jsfiddle.net/aP5A3/1/

I'm trying to build a way for a user to navigate through the items of a list #cityresults by navigating with the up/down arrows. When a user clicks the arrow down I want to apply class activedropdownitem on the active list item, and when he presses Return button while on that item I want to execute the same logic as when a user clicks with the mouse on that item, so in function $('#cityresults').on('click', 'a', function ().

I copied a Navigate function from another site, but have no idea how to properly implement it.

<input id="city" name="city" maxlength="50" autocomplete="off" class="textbox">
<input id="citygeonameid" name="citygeonameid" type="hidden">
<div id="cityresultscontainer">
    <div id="cityresults" style="display: block;">
        <ul>
        <li data-id="2750053"><a href="#" title="Nijmegen"><strong>Nij</strong>megen, Gelderland</a></li>
        <li data-id="2750065"><a href="#" title="Nijkerk"><strong>Nij</strong>kerk, Gelderland</a></li>
        <li data-id="2750089"><a href="#" title="Nijemirdum"><strong>Nij</strong>emirdum, Friesland</a></li>
        <li data-id="2750098"><a href="#" title="Nijega"><strong>Nij</strong>ega, Friesland</a></li>
        <li data-id="2990366"><a href="#" title="Nijon"><strong>Nij</strong>on, Champagne-Ardenne</a></li>
        <li data-id="2750094"><a href="#" title="Nijehaske"><strong>Nij</strong>ehaske, Friesland</a></li>
        <li data-id="2750104"><a href="#" title="Nij Altoenae"><strong>Nij</strong> Altoenae, Friesland</a></li>
        <li data-id="2750103"><a href="#" title="Nij Beets"><strong>Nij</strong> Beets, Friesland</a></li>
        <li data-id="8449187"><a href="#" title="Nijang"><strong>Nij</strong>ang, Nusa Tenggara Barat</a></li>
        <li data-id="2513222"><a href="#" title="Nijar"><strong>Nij</strong>ar, Andalusia</a></li>
        <li><span>Too many results? Refine your selection   </span></li>
        </ul>
    </div>
</div>



var Navigate = function (diff) {
     displayBoxIndex += diff;
     var oBoxCollection = $("#cityresults");
     if (displayBoxIndex >= oBoxCollection.length)
         displayBoxIndex = 0;
     if (displayBoxIndex < 0)
         displayBoxIndex = oBoxCollection.length - 1;
     var cssClass = "display_box_hover";
     oBoxCollection.removeClass(cssClass).eq(displayBoxIndex).addClass(cssClass);
 }

 window.displayBoxIndex = -1;
 $('#cityresults').on('click', 'a', function () {
     $('#city').val($(this).text());
     $('#cityresults').hide('');
     $('#citygeonameid').val($(this).parent().attr('data-id'));
     return false;
 });             

 $("#city").keyup(function (e) {
     if (e.keyCode == 37 || e.keyCode == 39) {
         return;
     }

     if (e.keyCode == 40) {
         //down arrow
         Navigate(1);
     }
     if (e.keyCode == 38) {
         //up arrow
         Navigate(-1);
     }

     if ($("#city").val().length > 1) {
        //here an ajax search is done

     }

 });

回答1:


OK so I'll post the code and example for you then type the explanation so you can have something while you wait. In the demo, make sure to click inside the display area or it won't capture your key presses (since JSFiddle uses an iframe).

Useful demo

Javascript:

$(document).ready(function () {
    window.displayBoxIndex = -1;
    $('#cityresults').on('click', 'a', function () {
        $('#city').val($(this).text());
        $('#cityresults').hide('');
        $('#citygeonameid').val($(this).parent().attr('data-id'));
        return false;
    });
    var Navigate = function (diff) {
        displayBoxIndex += diff;
        var oBoxCollection = $("#cityresults ul li a");
        if (displayBoxIndex >= oBoxCollection.length) {
            displayBoxIndex = 0;
        }
        if (displayBoxIndex < 0) {
            displayBoxIndex = oBoxCollection.length - 1;
        }
        var cssClass = "display_box_hover";
        oBoxCollection.removeClass(cssClass).eq(displayBoxIndex).addClass(cssClass);
    }
    $(document).on('keypress keyup', function (e) {
        if (e.keyCode == 13 || e.keyCode == 32) {
            $('.display_box_hover').trigger('click');
            return false;
        }
        if (e.keyCode == 40) {
            //down arrow
            Navigate(1);
        }
        if (e.keyCode == 38) {
            //up arrow
            Navigate(-1);
        }
    });
});

No change to the rest of the code.



来源:https://stackoverflow.com/questions/21062543/navigate-through-list-with-up-down-arrows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!