2012-04-11 39 views
1

这是我目前使用的代码:如何将函数应用于jquery ajax提交完成的类?

$.ajax({ 
    success: function(){ 
     $('.post_container').append('test <br />'); 
    } 
}); 
<% sleep 1 %> 

有使用同一类别中的几种意见,所以测试是它类似于我用我的单个主窗体微柱的代码,但与此适用于所有post_containers,而不是邮件刚刚制作的那个。 “测试”文本最终将被保存用户发布的实际评论的div替换。

通常我会使用“this”,但这不起作用。

HTML:

<div class="post_content"> 
    <div class="post_container"> 
     <div class="userNameFontStyle"> 
      <%= link_to current_users_username.capitalize, current_users_username %> - 
      <div class="post_time"> 
       <%= time_ago_in_words(m.created_at) %> ago. 
      </div> 
     </div> 
     <%= simple_format h(m.content) %> 
    </div> 
    <% if m.comments.any? %> 
    <% comments(m.id).each do |comment| %> 
    <div class="comment_container"> 
     <%= link_to image_tag(default_photo_for_commenter(comment), :class => "commenter_photo"), commenter(comment.user_id).username %> 
     <div class="commenter_content"> 
      <div class="userNameFontStyle"> 
       <%= link_to commenter(comment.user_id).username.capitalize, commenter(comment.user_id).username %> - <%= simple_format h(comment.content) %> 
      </div> 
     </div> 
     <div class="comment_post_time"> 
      <%= time_ago_in_words(comment.created_at) %> ago. 
     </div> 
    </div> 
    <% end %> 
    <% end %> 
    <% if logged_in? %> 
    <%= form_for @comment, :remote => true do |f| %> 
    <%= f.hidden_field :user_id, :value => current_user.id %> 
    <%= f.hidden_field :micropost_id, :value => m.id %> 
    <%= f.text_area :content, :placeholder => 'Post a comment...', :class => "comment_box", :rows => 0, :columns => 0 %> 
    <div class="commentButtons">   
     <%= f.submit 'Post it', :class => "commentButton" %> 
     <div class="cancelButton"> 
      Cancel 
     </div> 
    </div> 
    <% end %> 
    <% end %> 
    </div> 
</div> 

我将如何面对呢?

亲切的问候

+0

你能澄清你想要发生什么吗?你的代码不工作?您没有指定实际拨打电话的网址。 – Seth 2012-04-11 00:13:58

+0

是否像'$('。post_container')。submit(...)' – 2012-04-11 00:14:55

+0

我想简单地将新评论追加到评论列表中。我在Rails框架中使用ruby,大部分的ajax都是通过在表单中​​加入“remote => true”来设置的。有了上面的代码,如果ajax文章提交成功,那么我想将评论附加到评论列表。上面的代码附加到页面上的所有.post_container类,而不仅仅是我发布新评论的那个类。 – LondonGuy 2012-04-11 00:24:48

回答

0

这就是我最终要做的。

.new_comment是我的表格ID

$('.new_comment').on('ajax:success', function(){ 
    $(this).parent('.post_content').find('.comment_container:last').after("<BR />TEST <BR />"); 

}); 

<% sleep 1 %> 

唯一的问题现在查明后实际comment_container只是做。

0

新:

$(function() { 
    $('.post-form').submit(function(){ 
     var $post_content = $(this).find('.post_content'); 

     $.ajax({ 
      url: this.attributes['action'], 
      data: $(this).serialize(), 
      success: function() { 
       $post_content.append('test <br />'); 
      } 
     }) 
     return false; //this will stop the form from really submitting. 
    }); 
}); 

这应该做你要找的东西。只需在任何地方包含此JavaScript。

OLD:

$('form').submit(function(){ 
    $.ajax({ 
     url: this.attributes['action'], 
     data: $(this).serialize(), 

     //the below line might need to be adapted. 
     var post_content = $(this).find('.post_content'); 

     success: function() { 
      $(post_content).append('test <br />'); 
     } 
    }) 

这个假定只有一个页面上的形式。但是,要使代码完全工作,我需要知道如何区分哪个.post_content是“提交的”post_content。你如何以编程方式确定?每个post_content有不同的按钮吗?或者每个post_content div附近有不同的表单?无论如何,你必须以某种方式将它包含到js代码中。

+0

有1个表格,但它是“每个”循环的一部分,因此每个带注释的微博都会在下面显示一个表格。 – LondonGuy 2012-04-11 09:55:26

+0

然后这应该工作,不应该(与我添加的一个补充)?只需包含此js文件,并且每次提交带有“post-form”类的表单时,都会运行ajax请求。这对你有用吗? – 2012-04-11 16:37:37

+1

恩,我不认为你可以在那里声明你的post_content变量。你正在传递给ajax()函数的选项对象的中间。在开始你的ajax函数之前声明它。 – idrumgood 2012-04-11 16:44:46