简体   繁体   English

防止更改其他事件

[英]prevent other event keydown on change

on my textbox i have 2 events onchange and keydown(for enter) now i did validation on onchange event if any validation fail then i raise alert but when i press press enter button if validation failed then i dont want to execute keydown event 在我的文本框上,我有2个事件onchange和keydown(用于enter),现在我对onchange事件进行了验证(如果任何验证失败),然后我发出警报,但是当我按Enter键(如果验证失败)时,我不想执行keydown事件

any suggestion 任何建议

Thanks, Nik 谢谢,尼克

What you need to do is check the value of the key code on keydown for an invalid form. 您需要做的是在keydown上检查键码的值,以获取无效的表单。 Also define a local variable, assuming that the 2 functions are defined in the same context, if they are not use apply to call them in the same context. 还要定义一个局部变量,假设这两个函数是在相同的上下文中定义的,如果不使用它们,则应在相同的上下文中调用它们。

At that you can have a check if(event.keyCode != 13 && !invalid){ [Your operations] } 在那您可以检查if(event.keyCode != 13 && !invalid){ [Your operations] }

You can get more information about keycodes here . 您可以在此处获取有关键码的更多信息。

I hope this helps. 我希望这有帮助。

You would want to create a global variable (boolean) that your onchange event will alter. 您可能想要创建一个全局变量(布尔),您的onchange事件将更改。 Auto set that boolean to false, after it has been validated change it to true. 经过验证后,自动将该布尔值设置为false,然后将其更改为true。 After the keydown event is fired you will want to check and see if it was the enter key and that it passed all of your validation before taking any further action. 触发keydown事件后,您将要检查它是否是回车键,并在通过任何进一步的操作之前,它已通过所有验证。

<script type="text/javascript">
     var passedChecks = false;

     function validateText(e) {
          var src = (window.event) ? window.event.srcElement: e;

          if (src.value == b) { // verify your logic is true.
               passedChecks = true;
          } 
     }

     function validateKey(e) {
          var src = (window.event) ? window.event.srcElement: e; 

          if (src.keyCode == 13) {
               if (passedChecks == true) {
                    // process your code.
               }
          }
     }
</script>

<input type="text" id="myText" onchange="validateText()" onkeydown="validateKey()">

Not sure what exactly you want but if you want to control an keydown event you can do like this: 不确定您到底想要什么,但是如果您想控制按键事件,您可以这样:

<script type="text/javascript"> 

function onKeyDown() 

{  
   if(event.keyCode == 13 ) // if enter key  
   {  
// your validation-checking code here  
      if("validationfailed")  
      return false // prevent the event from happening if validation failed  
      else return true;// validating ok, allow it.  
   }  
   else  
   {  
      // another key, alow it.  
      return true;  
   }  
}  

</script>  
<input type="text" id="myText" onchange="validateText()" onkeydown="return onKeyDown()">  

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

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