简体   繁体   中英

javascript Uncaught ReferenceError: e is not defined

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

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 ?

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

onkeyup='evMoneyFormat( event );'

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

You don't need to pass anything to onkeyup .

It should just be: 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.

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

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

$(".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();
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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