Rails3:javascript在表单提交后使用.js.erb文件更新单个div
嗨,我是一个新的使用javascript和rails的人,希望能得到一些帮助。我有一个喜欢的按钮,我用它来喜欢和不喜欢一个帖子 home.html:Rails3:javascript在表单提交后使用.js.erb文件更新单个div,javascript,html,ruby-on-rails,ajax,Javascript,Html,Ruby On Rails,Ajax,嗨,我是一个新的使用javascript和rails的人,希望能得到一些帮助。我有一个喜欢的按钮,我用它来喜欢和不喜欢一个帖子 home.html: <%= form_for(current_user.appreciations. build(:liked_id => post.id), :remote => true) do |f| %> <div>&l
<%= form_for(current_user.appreciations.
build(:liked_id => post.id),
:remote => true) do |f| %>
<div><%= f.hidden_field :liked_id %></div>
<div class="actions"><%= f.submit "Like" %></div>
<% end %>
<div class="like-count<%= "_"+ post.id.to_s%>">
<%= user_post.likers.count.to_s %>
</div>
我使用一个js.erb文件在一篇文章被喜欢之后呈现这个不同的表单
create.js.erb:
$("#like_form_<%= @post.id.to_s %>").html("<%= escape_javascript(render(:partial => 'users/unlike', :locals => {:post => @post})) %>")
$("#like_form_<%= @post.id.to_s %>").html("<%= escape_javascript(render(:partial => 'users/unlike', :locals => {:post => @post})) %>"
$(".like-count_94").reset();
我想添加一行,在我的视图中更新div class=like count'
home.html:
<%= form_for(current_user.appreciations.
build(:liked_id => post.id),
:remote => true) do |f| %>
<div><%= f.hidden_field :liked_id %></div>
<div class="actions"><%= f.submit "Like" %></div>
<% end %>
<div class="like-count<%= "_"+ post.id.to_s%>">
<%= user_post.likers.count.to_s %>
</div>
我有许多帖子显示在一个提要中,我使用post.id.to_来选择要更新的特定帖子类计数。对于测试,我针对一个特定的post id=94,我使用下面的代码,但它似乎不起作用
新建create.js.erb:
$("#like_form_<%= @post.id.to_s %>").html("<%= escape_javascript(render(:partial => 'users/unlike', :locals => {:post => @post})) %>")
$("#like_form_<%= @post.id.to_s %>").html("<%= escape_javascript(render(:partial => 'users/unlike', :locals => {:post => @post})) %>"
$(".like-count_94").reset();
任何关于正确函数的输入或我出错的地方都会很好,非常感谢。如果您的like count div是唯一的,为什么不使用ID而不是类呢
$("#like-count").reset();
如果它确实需要是一个类,并且由于您正在获取第一个类,那么您可以对该方法使用以下语法:
我认为这不是正确的jQuery语法:
$(".like-count").[0].reset();
谢谢你的帮助,我正在尝试使用.reset,我在一个提要中有许多不同的帖子,需要使用.like在每个帖子上都作为一个类。我需要使用.like count,以便针对正确的帖子更新like count。我不想用文本替换div,而只是刷新要更新的内容,显示新的like。对于测试,我使用$.like-count_94.reset;但是,要针对特定的帖子,单击like按钮不会刷新.like-count_94 div并显示新的like编号。我已经更新了帖子以显示我的更改,谢谢!没问题!仅供参考,reset-方法-用于表单并将其重置为默认状态,即清除表单字段。我认为它在div上不起作用。我的感觉是,您需要text/html方法,它将替换现有的内容,或者追加将向div添加内容。您可能只需要在controller/.js.erb中重建div的内容,然后重新呈现它,即。,使用text或html方法。我尝试了一些使用.html和.reset的方法,但是我仍然不知道如何刷新div,我甚至尝试创建一个单独的partial来替换partial,但我无法获得“post”:本地工作:长话短说,reset无法满足您的需要。只需将表单字段重置为其原始/默认状态和值。html或.text是您所需要的。真的没有办法“刷新”你的div的内容。您必须重建它并将其添加到.js.erb中的DOM中。