简体   繁体   English

Rails jQuery仅加载到第一个元素

[英]Rails jquery only loaded to first element

I want to create a drop down with links to different controller actions. 我想创建一个包含指向不同控制器操作的链接的下拉菜单。 I have a helper method to provide options for my drop down. 我有一个帮助方法,可以为下拉菜单提供选项。

   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

I loop through each product and get options for each product. 我遍历每个产品并获得每个产品的选项。 but the problem is all products get their drop down loaded with their options but the click works only for the first product. 但是问题是所有产品的下拉菜单都带有选项,但是点击仅适用于第一个产品。 when i click drop down option for the rest nothing happens. 当我单击其余的下拉选项时,什么也没有发生。

I have jquery to handle the click event in the view 我有jQuery来处理视图中的点击事件

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

Any suggestions or solution or different approach would greatly help. 任何建议或解决方案或其他方法都会有很大帮助。 Thanks. 谢谢。

Yes that happens when you use same ids for multiple elements on a same document. 是的,当您对同一文档中的same ids for multiple elements使用same ids for multiple elements时,就会发生这种情况。 You have to use class instead. You have to use class

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

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

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

If you use same id notation for multiple elements in a same page then only first of the occurance will get the event not others. 如果对同一页面中的多个元素使用相同的id表示法,则只有第一个事件会获得该事件,而其他事件不会。

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

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