0

现在我正在使用 AJAX 创建评论。但是,当我点击输入时,文本字段仍然被填充。我希望该字段刷新(并且仍然可以在另一条评论中写)。没有错误,但它仍然没有刷新它看起来的文本字段。创建部分工作正常。

create.js/erb:(需要在这里修复第二行,以便完全替换)

$('#<%= dom_id(@micropost) %>').html("<%= escape_javascript(render(:partial => @micropost.comments))%>")
$("#comment_field_<%=@micropost.id%>").replaceWith("<%= escape_javascript(render 'shared/comment_form', micropost: @micropost) %>")

微博/_micropost:

<span id="<%= dom_id(micropost) %>"><%= render micropost.comments %></span>
<span id="comment_field_<%=micropost.id%>"><%= render 'shared/comment_form', micropost: micropost if signed_in? %></span>

共享/评论表格:

<%= form_for @comment, id:"comment_form", remote: true do |f| %>
  <%= hidden_field_tag :micropost_id, micropost.id %>
  <div id="comment_field">
    <%= link_to gravatar_for((current_user), size: 29) %>
    <%= f.text_field :content, placeholder: "Say Something...", id: "comment_text_field", :style => "width: 508px; text-indent: 5px" %>
  </div>
<% end %>
4

1 回答 1

0

只需要使用jquery通过设置空值来清除text_field

创建.js.erb

$("#comment_text_field_<%=@micropost.id%>").val("")

使用唯一动态 ID 的最佳实践,但无论哪种方式都可能有效。

共享/评论表格:

 <%= f.text_field :content, id: "comment_text_field_#{micropost.id}" %>
于 2013-02-27T08:49:29.217 回答