最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - javascript Uncaught ReferenceError: e is not defined - Stack Overflow

programmeradmin0浏览0评论

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'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 ?

Share Improve this question asked Jun 25, 2014 at 3:19 BishanBishan 15.8k53 gold badges172 silver badges268 bronze badges 2
  • 3 Well, what's e doing there? I'd suggest adding the event in JavaScript – elclanrs Commented Jun 25, 2014 at 3:20
  • Do you even need e here? – Nikhil Talreja Commented Jun 25, 2014 at 3:23
Add a ment  | 

2 Answers 2

Reset to default 4

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();
  }
});
发布评论

评论列表(0)

  1. 暂无评论