繁体   English   中英

Rails jQuery仅加载到第一个元素

[英]Rails jquery only loaded to first element

我想创建一个包含指向不同控制器操作的链接的下拉菜单。 我有一个帮助方法,可以为下拉菜单提供选项。

   def my_product_options(product)
    if product.status == Product::STATUS_INCOMPLETE
       my_options =  {"Select" => '', "Edit info" => edit_product_path(product)}
    elsif product.status == Product::STATUS_ACTIVE
       my_options =  {"Select" => '', "Edit info" => edit_product_path(product), "Suspend" => suspend_product_path(product)}
    end
    my_options
   end

我遍历每个产品并获得每个产品的选项。 但是问题是所有产品的下拉菜单都带有选项,但是点击仅适用于第一个产品。 当我单击其余的下拉选项时,什么也没有发生。

我有jQuery来处理视图中的点击事件

<script>
$('#create_options').change(function() {
    window.location = $(this).find('option:selected').val();
});
</script>

任何建议或解决方案或其他方法都会有很大帮助。 谢谢。

是的,当您对同一文档中的same ids for multiple elements使用same ids for multiple elements时,就会发生这种情况。 You have to use class

$('.create_options').change(function() {
 //^-----use class instead of id
    window.location = $(this).find('option:selected').val();
});

请参阅: http//jsfiddle.net/X5583/

In fiddle you can see both "using ids" and "using class" try commenting out each
one by one.

如果对同一页面中的多个元素使用相同的id表示法,则只有第一个事件会获得该事件,而其他事件不会。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM