簡體   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