事件未定义错误仅在 javascript Firefox 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11240874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 12:30:32  来源:igfitidea点击:

Event is not defined error in javascript only Firefox

javascriptvalidationfirefoxdom-events

提问by User

I use the following script to validate the text box to enter only numbers and (.) which means it is decimal textbox validation. It was work fine in Internet Explorer and Google Chrome. If I execute the function in FireFox I get the following Error:

我使用以下脚本来验证文本框以仅输入数字和 (.),这意味着它是十进制文本框验证。它在 Internet Explorer 和 Google Chrome 中运行良好。如果我在 FireFox 中执行该函数,则会出现以下错误:

Event Is not Defined.

事件未定义

How to solve this?

如何解决这个问题?

function abc(event) {

        if (event.keyCode > 47 && event.keyCode < 58) {
            return true;
        }
        if (event.keyCode == 8 || event.keyCode == 46)
        {
            return true;
        }
        return false;
    }

I call this function like this:

我这样称呼这个函数:

$('.decimalValidate').live('keypress',function(){
          var decimalid=$(this).attr("id");
          var decimalval=$('#'+decimalid).val();
          var decimalvalidate=abc(decimalval);
          if(decimalvalidate == false)
          return false;
     });

I assign this validation for text box like this:

我为文本框分配此验证,如下所示:

input type="text" id="Total" class="abc"

采纳答案by Clyde Lobo

Try this

试试这个

function abc(event) {
        if(!event)
           event= window.event;

        if (event.keyCode > 47 && event.keyCode < 58) {
            return true;
        }
        if (event.keyCode == 8 || event.keyCode == 46)
        {
            return true;
        }
        return false;
    }

and

$('.decimalValidate').live('keypress',function(e){
                              var decimalid=$(this).attr("id");
                              var decimalval=$('#'+decimalid).val();
                              var decimalvalidate=abc(evt); //keypress event
                              if(decimalvalidate == false)
                              return false;
                         });

回答by funerr

   $('.decimalValidate').live('keypress',function(e){
                              var decimalvalidate=abc(e); //this will point to the event of the keypress.
                              if(decimalvalidate == false)
                              return false;
                         });

I am not sure why you did all of the decimalid and decimalval operations, but if you want the event, do as I wrote in the edited code above.

我不确定你为什么要进行所有的decimalid和decimalval操作,但是如果你想要这个事件,就按照我在上面编辑过的代码中写的那样做。

Good luck.

祝你好运。

回答by davids

decimalval is not an Event object, and you have to pass it to the abc function in ordert to find out which key you pressed:

decimalval 不是 Event 对象,您必须将它传递给 abc 函数才能找出您按下的键:

$('.decimalValidate').live('keypress',function(ev){
                          var decimalid=$(this).attr("id");
                          var decimalval=$('#'+decimalid).val();
                          var decimalvalidate=abc(ev);
                          if(decimalvalidate == false)
                          return false;
                     });

回答by Prabhagaran

$('.decimalValidate').on('keypress',function(event){
     var decimalid       =  $(this).attr("id");
     var decimalval      =  $('#'+decimalid).val();
     var decimalvalidate =  abc(event);

     if(decimalvalidate == false)
        return false;
});



function abc(event) {

        if (event.keyCode > 47 && event.keyCode < 58) {
            return true;
        }
        if (event.keyCode == 8 || event.keyCode == 46)
        {
            return true;
        }
        return false;
}

It helps you..

它可以帮助你..