简体   繁体   English

使用Enter键清除警报框后如何停止触发onkeyup事件

[英]How to stop onkeyup event being fired when an alert box is cleared with the enter key

Here's my problem... 这是我的问题

I have a text field which uses the javascript onkeyup event to do some "quiet" validation with a tick or cross if the value is valid. 我有一个文本字段,该字段使用javascript onkeyup事件执行一个“安静的”验证,如果值有效,则用勾号或叉号进行验证。

When the enter key is pressed, it does the same validation, but with an alert box advising of any problems. 当按下回车键时,它会执行相同的验证,但是会出现一个警告框,提示任何问题。 If the enter key is used to clear the alert box, this press of the enter key refires the onkeyup event, which restarts the validation and the alert boxes again. 如果使用Enter键清除警报框,则按Enter键将重新激活onkeyup事件,该事件将重新启动验证并再次触发警报框。

As soon as I click the OK in the alert box, this naturally stops. 一旦单击警报框中的“确定”,该操作自然会停止。

Is there any way I can detect that the enter press was from the alert box? 有什么方法可以检测到输入框是否在警报框中?

This definitely happens in opera, safari and firefox. 这肯定发生在歌剧,野生动物园和Firefox。

触发回车键的行为时,请使用keypress事件。

Can you ignore the enter key in your onkeyup event handler? 您可以忽略onkeyup事件处理程序中的Enter键吗?

textbox.onkeyup = function( e ) {
    if( e.keyCode == 13 ) {
        return;
    }
    /* your validation here */
}

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

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