Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript在数组中一次只显示一个元素_Javascript_Jquery_Ruby On Rails_Arrays - Fatal编程技术网

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

javascript在数组中一次只显示一个元素,javascript,jquery,ruby-on-rails,arrays,Javascript,Jquery,Ruby On Rails,Arrays,因此,我希望在onclick时只显示一个元素 我发现这段代码很有用,不过我想稍微调整一下。让我告诉你我的意思: function() { var options = $('.some_class').hide(); $('.add').click(function() { if(options.length) { options.first().show(); options = options.slice(1); } }); }

因此,我希望在onclick时只显示一个元素

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

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数组中的第二个元素,同时只显示一个元素。

因此,您不需要提交表单,而只需要它显示@pets集合中的下一个宠物?问题是什么?是的!当有人提交特定宠物的表单时,它会在不重新加载页面的情况下发送表单,并显示阵列中的下一只宠物,之前的宠物应该消失