jQuery - 如何在组合框上实现 live()?

发布于 2024-09-29 04:03:57 字数 243 浏览 1 评论 0原文

我有组合框(事实上,其中有几个),其中包含动态添加的元素。

使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我还不会笑 2024-10-06 04:03:59

您在寻找这样的东西吗?

$(".foo").live("change", function() {
  $(this).val(); // value of the changed item
});

Are you looking for something like this?

$(".foo").live("change", function() {
  $(this).val(); // value of the changed item
});
请止步禁区 2024-10-06 04:03:59
$(".foo").live("change", function() {
   alert($(this).attr("id")); // Id of the element
});
$(".foo").live("change", function() {
   alert($(this).attr("id")); // Id of the element
});
星星的軌跡 2024-10-06 04:03:59

在 select 元素的子元素(也称为选项)上使用 :selected 选择器

$(".foo").live("change").function(){ 
    var val = $(".foo").find(":selected").val();
    // pass val to some other method for work
});

http ://api.jquery.com/selected-selector/

Use the :selected selector on child elements of the select element (a.k.a. options)

$(".foo").live("change").function(){ 
    var val = $(".foo").find(":selected").val();
    // pass val to some other method for work
});

http://api.jquery.com/selected-selector/

就是爱搞怪 2024-10-06 04:03:59

您可以使用 $(this).val() 查找触发事件的元素的值。

看来其他人已经打败了我。我的和@John 和@Daniel 一样。

这是一个用于测试它的 jsfiddle jsfiddle

需要注意的一点是,live 并不支持所有浏览器(例如 IE 6 到 8)中的更改方法。

解决这个问题的一种方法是使用委托方法,我已经在此处演示了该方法,

它看起来会有些东西喜欢:

$(parentElement).delegate(selector, 'change', function() {
    //do something interesting here
    //$(this).val() still works.
});

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:

$(parentElement).delegate(selector, 'change', function() {
    //do something interesting here
    //$(this).val() still works.
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文