jQuery的事件代表团在Chrome中选择/选项 [英] jQuery event delegation for select/options in Chrome

查看:96
本文介绍了jQuery的事件代表团在Chrome中选择/选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一个简单的单击事件下拉的选择的选项绑定到。

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');  
});

这code工作在Firefox,而不是浏览器/ Safari浏览器。我可以用.delegate()来绑定像这样的下拉菜单中的选项?如果是这样,怎么样?如果没有,什么是最好的替代解决方案?

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?

顺便说一句,<一个href=\"http://stackoverflow.com/questions/1402227/jquery-click-event-on-select-option-element-in-chrome\">jQuery点击事件.. 给出了使用.change()一个潜在的解决方案,但我想用.delegate()如果可以管理所有的绑定。

btw, jQuery Click on Event.. gives a potential solution using .change(), but I would like to manage all bindings with .delegate() if possible.

推荐答案

点击事件不火&LT;选项&gt;在这些浏览器元素,但你的选择是否正在发生变化与否,它没有什么不同有:

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());  
});

它归结到这一点,就没有必要对 .delegate() 在这里。 .delegate()用于侦听冒泡从可能改变或者是非常众多的,当元素本身不会去任何地方(例如,通过AJAX代替)子孙元素。 .live()在一个非常相似的方式的作品,它只是监听所有关于文档的方式向上

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.

我不知道一个更好的方式来解释它比这其他:您当前使用的根本不是 .delegate() 是,这是为了解决不同的问题。您应该使用 .change() 这里。

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.

这篇关于jQuery的事件代表团在Chrome中选择/选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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