如何取消Jquery中的箭头键

发布于 2024-12-04 13:21:35 字数 689 浏览 0 评论 0原文

我有这段代码用于 youtube 自动完成建议:

$(function(){

    $('#input-field').keyup(function(){
        var val = $(this).val();
        jQTubeUtil.suggest(val, function(response){
            var html = '';
            for(s in response.suggestions){
                var sug = response.suggestions[s];
                html += '<li class="li-class"><a href="#">'+sug+'</a></li>';
            }
            if (response.suggestions.length)
                $('#autocomplete').html(html).show();
            else 
                $('#autocomplete').hide();
        });
    });

});

如果按下箭头键(键码 40/38),如何停止发出请求? 我知道这与“!”有关。或事件“.not”,但我的努力没有得到回报。

I have this code which is for youtube autocomplete suggestions:

$(function(){

    $('#input-field').keyup(function(){
        var val = $(this).val();
        jQTubeUtil.suggest(val, function(response){
            var html = '';
            for(s in response.suggestions){
                var sug = response.suggestions[s];
                html += '<li class="li-class"><a href="#">'+sug+'</a></li>';
            }
            if (response.suggestions.length)
                $('#autocomplete').html(html).show();
            else 
                $('#autocomplete').hide();
        });
    });

});

How can I STOP a request being made if an arrow key (keycode 40/38) is being pressed?
I know it has something to do with '!' or event '.not' but my efforts did not pay off.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只是我以为 2024-12-11 13:21:35

如果键位于这些值之间,您可以简单地返回,从而有效地停止函数执行。

$(function(){
    $('#input-field').keyup(function(event){
        if (event.which >= 38 && event.which <= 40)
            return;

        ...
    });
});

You could simply return, effecitvely halting the function execution, if the key is between those values.

$(function(){
    $('#input-field').keyup(function(event){
        if (event.which >= 38 && event.which <= 40)
            return;

        ...
    });
});
沒落の蓅哖 2024-12-11 13:21:35

添加一个变量: $('#input-field').keyup(function(){ like e

$('#input-field').keyup(function(e){
    if(e.keyCode===38 /*Up key */ || e.keyCode === 40 /*key down */) { return ;}
    // Rest of your code...

请注意,您可能需要检查左侧和右侧也可以使用右键,但不要阻止它们,而是取消您对“其余代码”所做的调用:

$(function(){

    $('#input-field').keyup(function(e){

        if(!(e.keyCode>=37 && e.keyCode <= 40)) {
            var val = $(this).val();
            jQTubeUtil.suggest(val, function(response){
                var html = '';
                for(s in response.suggestions){
                    var sug = response.suggestions[s];
                    html += '<li class="li-class"><a href="#">'+sug+'</a></li>';
                }
                if (response.suggestions.length)
                    $('#autocomplete').html(html).show();
                else 
                    $('#autocomplete').hide();
            });
        }
    });

});

add an variable in: $('#input-field').keyup(function(){ like e

$('#input-field').keyup(function(e){
    if(e.keyCode===38 /*Up key */ || e.keyCode === 40 /*key down */) { return ;}
    // Rest of your code...

Note that you may want to have a check for the left and right keys also, but dont prevent those but cancel out the call you are doing on "the rest of the code":

$(function(){

    $('#input-field').keyup(function(e){

        if(!(e.keyCode>=37 && e.keyCode <= 40)) {
            var val = $(this).val();
            jQTubeUtil.suggest(val, function(response){
                var html = '';
                for(s in response.suggestions){
                    var sug = response.suggestions[s];
                    html += '<li class="li-class"><a href="#">'+sug+'</a></li>';
                }
                if (response.suggestions.length)
                    $('#autocomplete').html(html).show();
                else 
                    $('#autocomplete').hide();
            });
        }
    });

});
冰魂雪魄 2024-12-11 13:21:35

尝试此操作以取消一系列键:

$(function(){
    $('#input-field').keyup(function(e){
        if (e.which >= 38 && e.which <= 40)
            e.preventDefault();
    }
}

Try this to cancel a range of keys:

$(function(){
    $('#input-field').keyup(function(e){
        if (e.which >= 38 && e.which <= 40)
            e.preventDefault();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文