Jquery Rails 4中带有coffeescript的Div切换

Jquery Rails 4中带有coffeescript的Div切换,jquery,ruby-on-rails,coffeescript,Jquery,Ruby On Rails,Coffeescript,我有一个视图,其中对于一系列对象,我有几个div包含详细信息。对于每个div,我都有一个切换链接。我的问题是toggle()只适用于第一类div,即注册的第一次单击事件。代码如下: $(".class1").click (e) -> e.preventDefault() #get the index value I appended to this elements ID divIndex = $(this).attr("id").replace("link1_"

我有一个视图,其中对于一系列对象,我有几个div包含详细信息。对于每个div,我都有一个切换链接。我的问题是toggle()只适用于第一类div,即注册的第一次单击事件。代码如下:

$(".class1").click (e) ->
    e.preventDefault()

    #get the index value I appended to this elements ID
    divIndex = $(this).attr("id").replace("link1_", "")

    #select and toggle the respective message
    $("#div1_" + divIndex).toggle()
return

$(".class2").click (e) ->
    e.preventDefault()

    #get the index value I appended to this elements ID
    divIndex = $(this).attr("id").replace("link2_", "")

    #select and toggle the respective message
    $("#div2_" + divIndex).toggle()
return
它仅适用于注册的第一次单击事件。如果我改变顺序,另一个开始工作。对于第二个,它只是跟随视图中的链接


需要帮忙吗?谢谢

我假设此代码段来自另一个函数体?尝试删除
return
语句。因为它们不是嵌套在coffee脚本函数体中,所以它们引用的是顶层的函数