【问题标题】:Fadeout jquery not working. Deletes successfully but doesn't fade淡出 jquery 不起作用。删除成功但不褪色
【发布时间】:2014-01-03 22:20:48
【问题描述】:

这是我试图淡出的 div 的一部分。

<% @unconfirmed_sub_posts.each do |sub| %>
  <div class = "unconfirmed_post" id = "unconfirmed_sub_post_<%=sub.id%>">
   <table class = "table">
    <tr>
      <td id = "tablenoborder">
        <%= link_to 'x', sub, method: :delete, class:"btn btn-danger btn-small", remote: true %>
      </td>

请注意我放在 div 上的 ID,并且我将 remote:true 添加到了将其发送到销毁控制器操作的 link_to。

在我的控制器中,这里是销毁操作。

  def destroy
   @sub_opp.destroy
   respond_to do |format|
    format.html { redirect_to sub_opps_url }
    format.js
   end
 end

最后,在我的 destroy.js.erb 文件中:

$('#unconfirmed_sub_post_<%= @sub_opp.id %>').fadeOut(500);

项目正在被删除,但并未淡出。当我刷新页面时,div 消失了。 Javascript 控制台给了我这个错误:

DELETE http://localhost:3000/sub_opps/14 500 (Internal Server Error) 

呈现的 HTML:

<div class = "unconfirmed_post" id = "unconfirmed_sub_post_14">
  <table class = "table">
    <tr>
      <td id = "tablenoborder">
        <a class="btn btn-danger btn-small" data-method="delete" data-remote="true" href="/sub_opps/14" rel="nofollow">x</a>
      </td>
      <td id = "tablenoborder">
        <b>Basketball</b> at <b>depaul</b> on <b>Saturday, January 04, 2014</b>  at <b> 4:00pm</b>.
      </td>
    </tr>
  </table>
  <!-- ANY RESPONSES TO PENDING SUB POSTS -->
  <div class = "responses_to_posts">
    <h5 class = "nomargintop">Responses</h5>
        <p class = "nonheadingtext">There are no responses yet.</p>
  </div>
</div>

任何想法为什么? 谢谢

【问题讨论】:

  • 你能分享你渲染的 HTML 和 JS 而不是服务器端代码吗?
  • @brad 你用我下面的回答解决了你的问题吗?

标签: javascript ruby-on-rails ajax jquery


【解决方案1】:

调用破坏的 javascript 在哪里? fadeOut 接受回调。尝试在回调中调用调用破坏服务器的代码。 像这样

 $('#unconfirmed_sub_post_<%= @sub_opp.id %>').fadeOut(500, function(){
    //put your code that calls the server to destroy here
 });

【讨论】:

  • 谢谢 - 我确定就是这样。我正在尝试研究此代码将用于回调 - 对 ajax 来说非常新。谢谢
【解决方案2】:

在提交表单时,您需要先使用fadeOut(),然后在fadeOut() 回调中允许提交表单。

【讨论】:

    【解决方案3】:

    问题是我的 javascript 文件位于错误的视图子文件夹下。我试图从用户仪表板中删除一个帖子,并将 js 文件放在用户目录中,而实际上它应该在 post 子文件夹中。

    经验教训:确保 js 文件位于您正在修改的对象的视图文件夹下,而不是链接所在的位置。

    感谢您的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多