FadeOut一个div标签和他的内容

时间:2014-04-17 13:50:24

标签: javascript jquery ruby-on-rails html

如果点击“删除”链接,我想fadeOut一个div-tag及其嵌套内容。

我写了以下代码:

控制器:

  respond_to :html, :js
   def destroy_attachment
   @div_id = params[:id]

   respond_to do |format|
    respond_with @div_id
   end
  end

查看:

  <% for attachment in attachments %>
    <div class="attachment_<%= attachment.id %>">
            .....................
            ...some other content...
            .....................               
    <%= link_to "Destroy",  {controller: "topics" , action: "destroy_attachment"},
        :id => attachment.id,:remote => true, :confirm => 'Are you sure?', 
         :method => :post ,  class: "icon icon-del" %>
    </div>
  <% end %>

destroy_attachment.js.erb:

  $('.attachment_<%= @div_id %>').fadeOut("slow");

我做错了什么? selectioncall(*.attachment_<%= @div_id %>*)文件中的.js是错误的还是别的?

1 个答案:

答案 0 :(得分:0)

所以我在我的实现中发现了一些错误,现在它适用于我,使用以下代码:

<强>控制器:

def destroy_attachment
  @div_id =  params[:id]   
  respond_to do |format|
    format.html 
    format.js 
  end
end

查看:

<%= link_to "Destroy",  {controller: "topics" , action: "destroy_attachment",:id => attachment.id}, :remote => true, :confirm => 'Are you sure?', :method => :post ,  class: "icon icon-del" %>

在这里,我在大括号中移动了“:id =&gt; attachment.id”部分,以便使用Controller中的params方法进行访问

<强> destroy_attachment.js.erb:

$("#attachment_<%=@div_id%>").fadeOut(150, function(){
  $(this).remove();
});