javascript未捕获的ReferenceError: e未定义

时间:2021-12-16 19:01:06

I'm getting Uncaught ReferenceError: e is not defined.

我得到的是未捕获的ReferenceError e没有定义。

Input field

输入字段

<input class="form-control text-right" name="amount" maxlength="45" value="${exp.Amount}" onkeyup='evMoneyFormat( e );'  required="required">

Script

脚本

<script type="text/javascript">

function evMoneyFormat(evt) {
    //--- only accepts accepts number and 2 decimal place value
    var theEvent = evt || window.event;
    var key = theEvent.keyCode || theEvent.which;
    key = String.fromCharCode(key);
    var regex = /^[0-9]{1,14}\.[0-9]{0,2}$/; // number with 2 decimal places
    if (!regex.test(key)) {
        theEvent.returnValue = false;
        //--- this prevents the character from being displayed
        if (theEvent.preventDefault) theEvent.preventDefault();
    }
}
 </script>

How can i fix this ?

我该怎么解决这个问题呢?

2 个解决方案

#1


2  

You don't need to pass anything to onkeyup.

你不需要传递任何东西到onkeyup。

It should just be: onkeyup='evMoneyFormat()';.

它应该是:onkeyup='evMoneyFormat()';

When your handler is called, if you have provided a parameter to your function (which you have: evt), then the event data will automatically be assigned to the parameter.

当调用处理程序时,如果向函数提供了一个参数(有:evt),则事件数据将自动分配给该参数。

You can then use evt in your handler for the event data.

然后可以在事件数据的处理程序中使用evt。

However, seeing as you've tagged this with jQuery, a simpler way would be:

但是,如果您已经用jQuery标记了这一点,那么更简单的方法是:

$(".form-control text-right").keyup(function(evt){
  //--- only accepts accepts number and 2 decimal place value
  var theEvent = evt || window.event;
  var key = theEvent.keyCode || theEvent.which;
  key = String.fromCharCode(key);
  var regex = /^[0-9]{1,14}\.[0-9]{0,2}$/; // number with 2 decimal places
  if (!regex.test(key)) {
      theEvent.returnValue = false;
      //--- this prevents the character from being displayed
      if (theEvent.preventDefault) theEvent.preventDefault();
  }
});

#2


2  

I think it should be(the event object is available in the inlined context as event not as e)

我认为它应该是(事件对象在内联上下文中作为事件而不是作为e)

onkeyup='evMoneyFormat( event );'

Since you have tagged it with jQuery, use a jQuery event handler instead of inlined one

因为您已经用jQuery标记了它,所以使用jQuery事件处理程序而不是内联事件处理程序

#1


2  

You don't need to pass anything to onkeyup.

你不需要传递任何东西到onkeyup。

It should just be: onkeyup='evMoneyFormat()';.

它应该是:onkeyup='evMoneyFormat()';

When your handler is called, if you have provided a parameter to your function (which you have: evt), then the event data will automatically be assigned to the parameter.

当调用处理程序时,如果向函数提供了一个参数(有:evt),则事件数据将自动分配给该参数。

You can then use evt in your handler for the event data.

然后可以在事件数据的处理程序中使用evt。

However, seeing as you've tagged this with jQuery, a simpler way would be:

但是,如果您已经用jQuery标记了这一点,那么更简单的方法是:

$(".form-control text-right").keyup(function(evt){
  //--- only accepts accepts number and 2 decimal place value
  var theEvent = evt || window.event;
  var key = theEvent.keyCode || theEvent.which;
  key = String.fromCharCode(key);
  var regex = /^[0-9]{1,14}\.[0-9]{0,2}$/; // number with 2 decimal places
  if (!regex.test(key)) {
      theEvent.returnValue = false;
      //--- this prevents the character from being displayed
      if (theEvent.preventDefault) theEvent.preventDefault();
  }
});

#2


2  

I think it should be(the event object is available in the inlined context as event not as e)

我认为它应该是(事件对象在内联上下文中作为事件而不是作为e)

onkeyup='evMoneyFormat( event );'

Since you have tagged it with jQuery, use a jQuery event handler instead of inlined one

因为您已经用jQuery标记了它,所以使用jQuery事件处理程序而不是内联事件处理程序