jQuery - 如何在组合框上实现 live()?
我有组合框(事实上,其中有几个),其中包含动态添加的元素。
使用 jQuery,我不知道如何实现返回我在组合中选择的项目 id 的函数...
我知道它必须是使用 .live() 的东西,比如
$(".foo").live("change", function() {
do something;
});
...但我不知道这里如何实现。
tnx 在广告中!
i have combobox (in fact, several of them), with elements that are added dynamicly.
using jQuery, i don't know how to implement function that will return id of item i selected in combo...
i know that it have to be something using .live(), something like
$(".foo").live("change", function() {
do something;
});
... but i don't know how to implement it here.
tnx in adv!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您在寻找这样的东西吗?
Are you looking for something like this?
在 select 元素的子元素(也称为选项)上使用
:selected
选择器http ://api.jquery.com/selected-selector/
Use the
:selected
selector on child elements of the select element (a.k.a. options)http://api.jquery.com/selected-selector/
您可以使用
$(this).val()
查找触发事件的元素的值。看来其他人已经打败了我。我的和@John 和@Daniel 一样。
这是一个用于测试它的 jsfiddle jsfiddle。
需要注意的一点是,live 并不支持所有浏览器(例如 IE 6 到 8)中的更改方法。
解决这个问题的一种方法是使用委托方法,我已经在此处演示了该方法,
它看起来会有些东西喜欢:
You can use
$(this).val()
to find the value of the element that fired the event.It seems others have beat me to it. Mine is the same as @John and @Daniel.
Here is a jsfiddle to test it out jsfiddle.
One thing to note is that the live does not support the change method in all browsers (such as IE 6 through 8).
One way around this is to use the delegate method, which I have demonstrated here
It would look something like: