【问题标题】:document keydown bind not working for input field文档 keydown 绑定不适用于输入字段
【发布时间】:2016-04-29 18:25:38
【问题描述】:

我一直在使用来自jeresig's hotkey 的 jquery 热键插件。当文档处于焦点时,快捷方式可以正常工作,但是当焦点在输入字段中时,快捷方式不起作用。我使用$(document)$(document).find('input') 进行绑定。但这些也不起作用。

我使用以下代码制作快捷方式:

$(document).ready(function(){
        shortcutsInit(); 
});

function shortcutsInit(){
    $(document).bind('keydown', "shift+f2", function() {
        window.location.replace("/list");
        return false;
    });

    $(document).bind('keydown', "f3", function() {
        if($('#searchholder').length){
           $('#searchholder').focus();
        }
        console.log('f3 pressed');
        return false;
    });
}

【问题讨论】:

    标签: javascript jquery jquery-hotkeys


    【解决方案1】:

    试试看:

    $(document).ready(function(){
        $(document).on("keydown", function(e){
            if(e.shiftKey && (e.which || e.keyCode || e.charCode) == 113){
                    window.location.replace("/list");
                    return false;
            }
            if((e.which || e.keyCode || e.charCode) == 114){
                if($('#searchholder').length)
                    $('#searchholder').focus();
                console.log('f3 pressed');
                return false;
            }
        });
    });
    

    【讨论】:

    • 不是基于我的问题,但感谢您的回答。这是另一种解决方案,并且有效。
    【解决方案2】:

    也许这些选项正在解决问题:

    $.hotkeys.options.filterInputAcceptingElements = false;
    $.hotkeys.options.filterTextInputs = false;     
    

    【讨论】:

    • 在哪里设置这个选项?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 2021-03-11
    相关资源
    最近更新 更多