Trigger an event when textbox contains specific keycode

I am currently working on a jQuery functionality to check a textbox for certain character values and then trigger some event depending on what character it is. I figured the easiest way to process this would be by checking on keycodes. I have tried some different things to make the event trigger, but nothing seems to happen. One of the things I want to check is if a textbox contains an `@` character (which is not allowed in the specific textbox, and it's keycode is 64 according to this site - [expandinghead][1]) Some of the code I've attempted to use: $("#NoEmailAllowed").live({ "keyup": function(e) { if((e.keyCode == 64) || (e.which == 64)) { $(this).addClass("redBg"); } } }); $("#NoEmailAllowed:contains('@')").live({ "keyup": function() { $(this).addClass("redBg"); } }); $("#NoEmailAllowed").keyup(function(){ if(this.val().contains("@")) { $(this).addClass("redBg"); } }); I've tried a lot of others too, but they were quiet similar to the ones above. So just to make everything clear about what I'm trying to achieve is: When a user enters something in this textbox the jquery shall check for any `@` characters on-the-fly, and if it finds any `@` characters, trigger an event - for instance an alert or add a css class. [1]:

以上就是Trigger an event when textbox contains specific keycode的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准