所选类的jQuery绑定事件 [英] Jquery binding event on selected class

查看:123
本文介绍了所选类的jQuery绑定事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是jQuery中实现一个事件,以一组具有一定的阶级控制的绑定?在我看来,它不能。我google了一下,说想出都是无关的事件。这里是我的code的样子 -

  $('。numonly')。绑定(键preSS',函数(事件){
    如果(event.which&GT 31安培;及(event.which&下; 48 || event.which> 57))返回false;
});


解决方案

您code应该工作,这里是它在行动的例子: http://jsfiddle.net/g3GsE/

请确保您的code被包裹这样的,因此它不会执行,直到的document.ready

  $(函数(){
  $('。numonly')。绑定(键preSS',函数(事件){
    如果(event.which&GT 31安培;及(event.which&下; 48 || event.which> 57))返回false;
  });
});

如果没有这个,它会立即执行与类=numonly元素不会在那里找到尚未...的code需要等待直到的document.ready所以它触发后的元素都在那里,所以选择找到它们。

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 && (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.

这篇关于所选类的jQuery绑定事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆