Chrome中的select / options的jQuery事件委派 [英] jQuery event delegation for select/options in Chrome
问题描述
$('select# myelect')。delegate(':selected','click',function()
{
alert('selected');
});
此代码适用于Firefox,但不适用于Chrome / Safari。我可以使用.delegate()绑定下拉菜单的选项吗?如果是这样,怎么办?如果没有,最好的替代方案是什么?
btw, jQuery点击事件.. 给出了使用.change()的潜在解决方案,但是如果可能,我想使用.delegate()来管理所有绑定。
点击
事件不会触发这些浏览器中的< option>
元素,但是您的选项正在改变或不,它没有什么不同:
$('#myselect')。change(function() b $ b alert('selected,my new value is:'+ $(this).val());
});
根据这一点,不需要 .delegate()
这里。 .delegate()
用于收听 .live()
的工作原理非常相似方式,它只是收听文档
。
我不知道一个更好的解释方法除此之外:您当前的使用情况根本不是什么 .delegate()
是为了解决一个不同的问题。您应该在这里使用 .change()
。
I am trying to bind a simple click event to an the selected option of a pulldown.
$('select#myselect').delegate(':selected', 'click', function()
{
alert('selected');
});
This code works in Firefox, but not Chrome/Safari. Can I use the .delegate() to bind an option for a pulldown menu like this? If so, how? If not, what is the best alternate solution?
btw, jQuery Click on Event.. gives a potential solution using .change(), but I would like to manage all bindings with .delegate() if possible.
The click
event doesn't fire for <option>
elements in these browsers, but whether your options are changing or not, it there's nothing different with:
$('#myselect').change(function() {
alert('selected, my new value is: ' + $(this).val());
});
It comes down to this, there's no need for .delegate()
here. .delegate()
is used to listen for events that bubble up from descendant elements that may change or are very numerous, when the element itself won't be going anywhere (e.g. replace via AJAX). .live()
works in a very similar way, it just listens all the way up on document
.
I don't know a better way to explain it other than this: your current usage is not at all what .delegate()
is for, it's intended to solve a different problem. You should use .change()
here.
这篇关于Chrome中的select / options的jQuery事件委派的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!