如何使用JavaScript捕捉关键重复 [英] how to capture key repeats with javascript

查看:116
本文介绍了如何使用JavaScript捕捉关键重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net表单和一个asp:文本框。当用户按下并按住某个键时,我遇到问题。用户选择文本框,然后按住'9'直到文本框填充9。

i have an asp.net form and an asp:textbox. i have a problem when the user presses and HOLDS a key down. the user selects the text box and then presses and holds '9' until the text box fills with 9s.

有什么办法可以检测到这种情况吗?
当键被按下时,有没有办法停止键重复?

Is there any way to detect this situation? Is there a way to stop key repeats when the key is held down?

推荐答案

使用jquery,你可以做像这样:

Using jquery, you could do something like this:

<script type="text/javascript">

    var allowKey = true;

    $(function () {
        $("#one").keydown(function (e) {
            if (!allowKey) {
                e.preventDefault();
            }
            allowKey = false;
        });

        $("#one").keyup(function () {
            allowKey = true;
        });
    });

</script>

<input id="one" />

已更新的错误修正:

    var keyCount = 0;
    $(function () {
        $("#one").keypress(function (e) {
            if (keyCount > 1) {
                e.preventDefault();
            }
            keyCount++;
        });

        $("#one").keyup(function () {
            keyCount = 0;
        });
    });

这篇关于如何使用JavaScript捕捉关键重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆