简体   繁体   English

如何用键盘上的键“c”键盘快捷键来调用一个功能

[英]how to make keyboard shortcut with key “c” from keyboard for calling a function

I am wondering if it is possible to call a jquery function from keyboard key. 我想知道是否可以从键盘键调用jquery函数。

Say i want to clear textbox values , if users press "c" key from her keyboard. 假设用户按下键盘上的“c”键,我想清除文本框值。

Now i am calling a function onclick event.Same function i want to call if users press "c" key from keyboard 现在我正在调用一个函数onclick event.Same函数我想调用如果用户从键盘按“c”键

  $(".clear").click(function()
        { 
            $('#from_amount').val(''); 
            $('#to_amount').val(''); 
            $('.clear').hide(); 
        });

Please help me to solve this problem 请帮我解决这个问题

Use the keypress event to handle this. 使用keypress事件来处理这个问题。

One possible problem with this will be, you won't be able to press c anywhere in the document, so you may want to support a key combination like ctrl + c . 一个可能的问题是,您将无法在文档中的任何位置按c ,因此您可能需要支持ctrl + c等组合键。 Or do not want to do this when the key is pressed within a input field 或者在输入字段中按下键时不想这样做

<script>
    $(function(){
        $(document).keypress(function(e){
            if(e.which == 99){
                    $('#from_amount').val(''); 
                    $('#to_amount').val(''); 
                    $('.clear').hide(); 
            }
        })
    })
</script>

Ignoring keypress from input fields 忽略输入字段的按键

<script>
    $(function(){
        $(document).keypress(function(e){
            if(e.which==99 && !$(e.target).is(':input')){
                console.log('clear')
            }
        })
    })
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM