简体   繁体   English

如何在jQuery中阻止keyup()事件一秒钟?

[英]How can I block the keyup() event for a second in jQuery?

I'm doing a form validation in jQuery that alerts something when a forbidden character like #$@% is input by user. 我正在jQuery中进行表单验证,当用户输入#$ @%等禁用字符时会发出警告。 The problem is, when a user enters those characters, he uses the SHIFT key, so there are two keyup's. 问题是,当用户输入这些字符时,他使用SHIFT键,因此有两个键盘。

How can I prevent the second one from happening? 如何防止第二次发生?

if (!regex.test(lastCharacter)
     alert('forbidden character');

Thanks a lot 非常感谢

I have no idea if this will work, but you get the idea: 我不知道这是否有效,但你明白了:

$("#myInput").keyup(function(e) {
    if(e.which == 16) {
        $(this).keyup(function(event) {
            if(event.which==16) event.preventDefault();
        });

        setTimeout(function() { 
            $("#myInput").off('keyup');
        },1000);
    }
});​

You can bind on the input event which emits when input is changed, however this is only supported since IE9. 您可以绑定输入更改时发出的输入事件,但这仅在IE9之后才受支持。 Before that you can bind on the propertychange event (in IE only that is) which occurs when the value of an input changes. 在此之前,您可以绑定在输入更改时发生的propertychange事件(仅在IE中)。 The example below will work in most browser also it might trigger the callback twice in IE9 so might need to check for input event support and act accordingly instead. 以下示例适用于大多数浏览器,也可能在IE9中触发两次回调,因此可能需要检查输入事件支持并相应地执行操作。

http://jsfiddle.net/X4qNf/ http://jsfiddle.net/X4qNf/

My bad here is the one with propertychange in it, try in IE if it doesn't work you might have to assign it the old way. 我的坏处是在其中有属性更改的那个,如果它不起作用在IE中尝试你可能必须以旧的方式分配它。

 $('#boo')[0].onpropertychange = function() {};

http://jsfiddle.net/X4qNf/2/ http://jsfiddle.net/X4qNf/2/

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

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