javascript在数组中只显示一个元素

时间:2015-03-14 22:31:46

标签: javascript jquery ruby-on-rails arrays

所以我想在onc​​lick上只显示一个元素。

我发现这段代码很有用,但是我想稍微调整一下。让我告诉你我的意思:

function() {
  var options = $('.some_class').hide();

  $('.add').click(function() {
    if(options.length) {
      options.first().show();
      options = options.slice(1);      
    }
  });
})

这是我想逐个展示的元素:

<% @pets.each do |pet| %>
<table class="table">
    <thead>
        <tr>
            <th>Name</th>
            <th>Type</th>
            <th>Sex</th>
            <th>Age</th>
            <th>breed</th>
            <th>Size</th>
            <th>Picture</th>
            <th>Description</th>
            <th>Shelter_id</th>

            <th>Shelter</th>
            <th>Phone</th>
            <th>Email</th>
            <th>City</th>
            <th>Zip</th>
            <th>Like</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><%= pet.name %></td>
            <td><%= pet.species %></td>
            <td><%= pet.sex %></td>
            <td><%= pet.age %></td>

            <td><% pet.breeds.each do |breed| %>
                <li><%= breed.name %></li>
                <%end%>
            </td>
            <td><%= pet.size %></td>
            <td><%= image_tag pet.picture %></td>
            <td><%= pet.description %></td>
            <td><%= pet.shelter_id %></td>

            <% shelter = Shelter.find(pet.shelter_id)%>
            <td><%= shelter.name %></td>
            <td><%= shelter.phone %></td>
            <td><%= shelter.email %></td>
            <td><%= shelter.city %></td>
            <td><%= shelter.zip %></td>
            <td><%= form_for pet, :url => { :controller => "favorite_pets", :action => "create" }, :html => {:method => :post}, :remote => true do |f| %>
            <%= f.hidden_field :id %>
            <%= f.submit %>
            <% end %>
</td>
        </tr>


    </tbody>
</table>
<%end%>

因此,每当有人点击f.submit按钮时,javascript就会发挥作用,并在@pets数组中迭代第二个元素,只显示一个。

0 个答案:

没有答案