jQuery事件处理程序.on()不工作 [英] jQuery event handler .on() not working

查看:125
本文介绍了jQuery事件处理程序.on()不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想附加一个事件来动态创建的元素类。我使用的是活动的功能,但没有被触发。所以检查了实时功能参考,那里我红色的注释


从jQuery 1.7起,不推荐使用.live()方法。使用.on()到
附加事件处理程序。较旧版本的jQuery的用户应该使用
.delegate()优先于.live()。


所以决定使用 on 功能,但它仍然不工作。文本字段已经附加了jquery ui datpicker.On另一个元素选择我禁用该字段。

  jQuery(#from)。attr('disabled','disabled')
。 removeClass('date_picker_bg')
.removeClass('hasDatepicker')
.addClass('date_picker_disabled');

禁用后,如果我点击我想显示警报或tooltip.so我尝试过,但不工作

  jQuery(。date_picker_disabled)。on(click,function(event){
alert嗨');
});

可能是问题



我我使用jquery 1.7.1(jquery-1.7.1.min.js)

解决方案

问题是 jQuery(。date_picker_disabled)找到该类的元素并绑定到它们。如果元素在绑定时没有该类,则不会处理这些事件。



$ c>函数允许你通过在事件气泡到一个父元素的另一个元素处理它们时得到这个结果。在这种情况下,我们可以说正文元素 - 可能有一个更具体的常见父母可以选择。

  jQuery(document.body).on('click','.date_picker_disabled',function(event){
alert('hi');
}) ;

事件处理程序现在绑定到 document.body 元素。测试身体中任何地方发生的所有点击,以查看它们是否源自与选择器匹配的元素。如果是这样,处理程序将被触发。



有关 on 功能。它与以前版本的jQuery中存在的行为相同,即 live delegate 函数。






再次看看你的代码,你有 disabled =disabled set在您的输入元素。 点击事件不会被禁用元素触发。


I want to attach a event to dynamically created element class.So i used live function but it was not triggered. So checked live function reference ,there i red below notes

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().

so decide to use on function,But it still not working.The text field is already attached with jquery ui datpicker.On another element select i disabled that field.

jQuery("#from").attr('disabled','disabled')
.removeClass('date_picker_bg')
.removeClass('hasDatepicker')
.addClass('date_picker_disabled');

after disabled if i click i want to show alert or tooltip.so i tried this,but not working

jQuery(".date_picker_disabled").on("click", function(event){
          alert('hi');
  });

What may be the problem

I am using jquery 1.7.1 ( jquery-1.7.1.min.js)

解决方案

The problem is that jQuery(".date_picker_disabled") finds elements with that class and binds to them. If elements don't have the class at the time the binding is made, the events will not be handled.

The on function allows you to get round this by handling them on another element when the event "bubbles up to" a parent element. In this instance, we could say the body element – there may be a more specific common parent you could choose.

jQuery(document.body).on('click', '.date_picker_disabled', function(event) {
    alert('hi');
});

The event handler is now bound to the document.body element. All clicks that happen anywhere in the body are tested to see if they originated from an element matching the selector. If so, the handler is fired.

This is explained on the documentation for the on function. It is the same behaviour as was present in previous versions of jQuery with live and delegate functions.


Having taken another look at your code, you have disabled="disabled" set on your input element. click events are not fired on disabled elements.

这篇关于jQuery事件处理程序.on()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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