Javascript 将CSS id插入jQuery
我有一个基本的评论。每个评论的陈述,在它们的底部是一个添加新评论的表单。目标很简单,但我已经尝试了很多次,无法实现。当有人在表单中提交评论时,我希望该评论显示在上面,并使用jQuery将表单向下移动。这很好,但我有两个问题Javascript 将CSS id插入jQuery,javascript,jquery,ruby-on-rails,Javascript,Jquery,Ruby On Rails,我有一个基本的评论。每个评论的陈述,在它们的底部是一个添加新评论的表单。目标很简单,但我已经尝试了很多次,无法实现。当有人在表单中提交评论时,我希望该评论显示在上面,并使用jQuery将表单向下移动。这很好,但我有两个问题 我需要jQuery显示的新注释来继承名为triangle right的CSS类 我需要的形式,以明确,它保留在那里提交后的最后评论 这是jQuery。同样,我需要@comment.recommendation使用CSS类三角形右边显示,以便表单清除#newrec是表单 $('
$('#newrec').before('<%= escape_javascript(@comment.recommendation) %>');
结束
谢谢 解决方案是:
部分提取为部分$('ul#comments').append('<%=j render partial: "comment", locals: {comment: @comment}')
$('#comment_form').html('<%=j render partial: "comment_form",
locals: {comment: Comment.new)')
$('ul#comments').append('请显示与此问题相关的完整代码。您已经显示了js.erb,请显示视图模板和控制器代码。此外,“建议”是什么?我将继续添加代码。建议是我的rails应用程序中注释表中的文本列。
def create
@comment = Comment.new
@comment.recommendation = params[:recommendation]
@comment.trip_detail_id = params[:trip_detail_id]
@comment.user_id = params[:user_id]
if @comment.save
respond_to do |format|
format.html { redirect_to :back, notice: 'Rec was successfully created.' }
format.json { render action: 'show', status: :created, location: @comment }
format.js
end
else
render 'new'
end
$('ul#comments').append('<%=j render partial: "comment", locals: {comment: @comment}')
$('#comment_form').html('<%=j render partial: "comment_form",
locals: {comment: Comment.new)')