原型切换仅通过数组切换最后一项

时间:2010-01-24 06:06:35

标签: javascript ruby-on-rails ruby prototype

我是初学者,我哥哥给我看了一些原型。所有周都成功切换阵列中的最后一周,但没有其他人。如果我需要更具体,请告诉我!谢谢!

<%= @athletic_program.program_name %>
<br>
<br>

<% array of_ids = Array.new %>

<% @program_weeks.each do |program_week| %>
  <% array_of_ids.push(program_week.id) %>
  <div id="week_number_<%= program_week.id %>" class="week_number">Week <%=program_week.week_number %></div>
<div id="program_week_form_<%= program_week.id %>" class="program_week_form">
    <% form_for(program_week) do |f| %>
      <%= f.error_messages %>
      Number of Workouts <%= f.select :number_of_workouts, (1..7), {} %>
      <%= f.submit 'Next' %>
    <% end %>
  </div>
<% end %>

<% content_for :javascript do %>
<script type="text/javascript">
function showForms(array_of_ids){
  for(var i=0;i<array_of_ids.length;i++){
    id = array_of_ids[i];
    week_id = "week_number_" + array_of_ids[i];
    $(week_id).onclick = function(event){
      program_form = "program_week_form_" + id;
      form_div = $(program_form);
      if (form_div.style.display == 'none'){
        form_div.style.display = 'block';
      } else {
        form_div.style.display = 'none';
      }
    }
  }
}

showForms(<%= array_of_ids.to_json %>);
</script>
<% end %>

以下现在有效!

<%= @athletic_program.program_name %>
<br>
<br>

<% @program_weeks.each do |program_week| %>
  <div id="week_number_<%= program_week.id %>" class="week_number" onclick="$('program_week_form_<%= program_week.id %>').toggle()">Week <%=program_week.week_number %></div>
  <div id="program_week_form_<%= program_week.id %>" class="program_week_form" style="display:none;">
    <% form_for(program_week) do |f| %>
      <%= f.error_messages %>
      Number of Workouts <%= f.select :number_of_workouts, (1..7), {} %>
      <%= f.submit 'Next' %>
    <% end %>
  </div>
<% end %>

我添加了style =“display:none;”到program_week_form_默认隐藏它。

1 个答案:

答案 0 :(得分:1)

您是否只是在点击周div时尝试切换表单?在那种情况下,你真的是以错误的方式解决这个问题。

我认为你可以删除你所有的javascript和你的array_of_ids,如果你使用的是原型,只需将周div更改为:

<div id="week_number_<%= program_week.id %>" class="week_number" onclick="$('program_week_form_<%= program_week.id %>').toggle()">Week <%=program_week.week_number %></div>