开发者

Jquery binding event on selected class

开发者 https://www.devze.com 2022-12-25 09:08 出处:网络
Is it achievable in jquery to bind an event to a group of control that has certain class? It seems to me, it can\'t. I google a bit and all that came up are nothing to do with events. Here\'s how my c

Is it achievable in jquery to bind an event to a group of control that has certain class? It seems to me, it can't. I google a bit and all that came up are nothing to do with events. Here's how my code looks -

$('.numonly').bind('keypress',function(event){
    if (event.which > 31 &am开发者_如何学Gop;& (event.which < 48 || event.which > 57)) return false;
});


Your code should work, here's an example of it in action: http://jsfiddle.net/g3GsE/

Make sure that your code is wrapped like this so it doesn't execute until document.ready:

$(function() {
  $('.numonly').bind('keypress',function(event){
    if (event.which > 31 && (event.which < 48 || event.which > 57)) return false;
  });
});

Without this, it would execute immediately and the class="numonly" elements won't be there to find yet...the code needs to wait until document.ready so it fires after the elements are there, so the selector finds them.


Yes this code should work just fine. Just make sure you double check it on the server when submitted. So those who disabled JS can't get around your limitation.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号