0

我有一个文本框,在单击或输入单词时,用户会看到一些建议,问题是建议可见并且使用文本框内的向上/向下键导航到建议也是它的导航

下面是代码:

<label>when</label>
                        <div class="fl">
                            <input class="date-field startdate cornerall" type="text"/>
                            <div class="input-wrapper">
                            <input type="text" class="starttime cornerall" placeholder="Add a Time?"/>
                            <div class="dropdown stsugstn hidden">
                            <ul class="result cornerall">
                                <li class="sugactive">11.05</li>
                                <li>11.05</li>
                                <li>11.05</li>
                                <li>11.05</li>
                                <li>11.05</li>
                            </ul>
                            </div>
                            </div>
                            <a href="#" class="timezone title-tip" title="IST">GMT+5:30</a>
                            <a href="#" class="endtimeln">End Time?</a>               
                        </div>

各自的jQuery:

$('.cnewrap').on({
        keyup: function(e){
            var $this = $(this);
            var $vl = $this.val();
            var $st = $this.parent().find('.stsugstn');
            var key = (e.keyCode ? e.keyCode : e.which);
            if($vl != null && $vl.length > 0){
                if($st.hasClass( "hidden" )){
                    $st.removeClass('hidden');
                }
                if (key === 38 || key === 40){
                    //TODO write code to stop navigation inside text box
                    //$this.val($vl);
                }
            }else{
                if(!$st.hasClass( "hidden" )){
                    $st.addClass('hidden');
                }

            }
        },
        click: function(e){
            var $this = $(this);
            var $vl = $this.val();
            var $st = $this.parent().find('.stsugstn');
            if($vl != null && $vl.length > 0 && $st.hasClass( "hidden" )){
                $st.removeClass('hidden');
            }

        }
    },'.starttime');

用于在建议之间导航的 jquery:

$(window).on('keyup', function(e){
        var $stsugstn = $('.stsugstn');
        var $etsugstn = $('.etsugstn');
        var $next = '';
        var key = (e.keyCode ? e.keyCode : e.which);
        if($stsugstn!= null && $stsugstn.length > 0 && !$stsugstn.hasClass( "hidden" )){
            var $current = $('.stsugstn ul li.sugactive');
            if (key === 38){
                if($current.is(':first-child')){
                    $next = $('.stsugstn ul li:last-child');
                }else{

                    $next = $current.prev();
                }
            }
            if (key === 40){
                if($current.is(':last-child')){
                    $next = $('.stsugstn ul li:first-child');
                }else{
                    $next = $current.next();
                }
            }

            if ($next.length > 0) {
                $current.removeClass('sugactive');
                $next.addClass('sugactive');
            }
        }
        if($etsugstn!= null && $etsugstn.length > 0 && !$etsugstn.hasClass( "hidden" )){
            var $current = $('.etsugstn ul li.sugactive');
            if (key === 38){
                if($current.is(':first-child')){
                    $next = $('.etsugstn ul li:last-child');
                }else{

                    $next = $current.prev();
                }
            }
            if (key === 40){
                if($current.is(':last-child')){
                    $next = $('.etsugstn ul li:first-child');
                }else{
                    $next = $current.next();
                }
            }

            if ($next.length > 0) {
                $current.removeClass('sugactive');
                $next.addClass('sugactive');
            }
        }
    });

请建议我如何停止在文本框内导航,同时使用 jsfiddle 建议的向上/向下键在建议内导航,这里是链接 http://jsfiddle.net/Brg9t/

4

1 回答 1

0

触发你的事件keydown,那是因为浏览器行为已经完成了keyup,那个时候都不会preventDefault或者return false不会工作。

所以keyup改为keydown, 并添加return false//todo 部分就可以了。请参阅此处的小提琴http://jsfiddle.net/Brg9t/1/

于 2013-09-29T09:26:17.170 回答