问题描述
我想附加一个事件来动态创建的元素类。我使用的是活动的功能,但没有被触发。所以检查了,那里我红色的注释所以决定使用功能,但它仍然不工作。文本字段已经附加了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上的 $ c>函数允许你通过在事件气泡到一个父元素的另一个元素处理它们时得到这个结果。在这种情况下,我们可以说
正文
元素 - 可能有一个更具体的常见父母可以选择。
jQuery(document.body).on('click','.date_picker_disabled',function(event){
alert('hi');
}) ;
事件处理程序现在绑定到 document.body
元素。测试身体中任何地方发生的所有点击,以查看它们是否源自与选择器匹配的元素。如果是这样,处理程序将被触发。
有关功能。它与以前版本的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
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()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!