gpt4 book ai didi

javascript - 无法通过 Jquery 1.4 用于 ajax 实现的覆盖提交方法清除文本字段

转载 作者:行者123 更新时间:2023-12-02 20:17:36 24 4
gpt4 key购买 nike

我有一个功能,可以通过 Jquery 使用 AJAX 将消息发布到一个组。当我单击“发布”按钮(即submit_tag 的值)时,将呈现javascript,并且无需重新加载的页面将显示发布的最新消息。基本上调用 post_message.js.erb 文件。我正在利用以下教程来实现相同的功能:- http://railscasts.com/episodes/136-jquery

post_message.js.erb 的代码如下所示:-

$("#new_post").before('<div id ="new_post"><%= escape_javascript(flash.delete(:notice)) %></div>');<!--TO-DO the flash messages aren't yet enabled/working-->
$("#latest_post").prepend("<%= @group_post.message %> by <%= Investor.find(@group_post.post_by).first_name %> <div class=contentdispgrp> <%= distance_of_time_in_words(@group_post.created_at,Time.now) %> ago</div> <br/><br/><hr/>");
$("#post_msg")[0].reset();

表单中原本会渲染 HTML 并通过重新加载页面显示已发布消息的部分如下所示:-

<%form_for  :group_post, @group_post, :url => {:action => :post_message, :id => params[:id]},:html => {:multipart => 'true', :id => 'new_post'},:id => 'post_form' do |f| -%>
Start Discussion:<label id="post_msg"><%=f.text_field :message%></label>
<!--<%=f.file_field :photo%> -->
<%=submit_tag "Post" %></p>
<!--<input type="button" value="Reset Form" onClick="this.form.reset()" />-->
<%end%>

<div id = "latest_post"> </div>
<%for a in @group_all_posts %>
<%= a.message %> by <%= Investor.find(a.post_by).first_name %> <div class ="contentdispgrp" id="style_chck"> <%= distance_of_time_in_words(a.created_at,Time.now) %> ago </div><br/><br/> <hr/>
<%end%>


</tr>

我想清空文本字段以及上面已经实现的功能。我想,我需要一些如何使用 Javascript reset 方法,但在某个地方我没有正确使用它。

请帮助我。

感谢您的帮助..

[编辑]

我的组 Controller 中的 post_message 代码。

def post_message 
@investor_group = InvestorGroup.find(params[:id])


unless @current_user.is_an_existing_member_of_group(@investor_group)
flash[:notice] = "Please join this group to participate in discussions"
redirect_to :action => :show, :id => @investor_group and return # try to find out what exactly does this command do with return stmnt
else
@group_post = GroupPost.new(params[:group_post])
end
#@group_post = GroupPost.new(params[:group_post])

investor_id = session['investor_id']
@group_post.investor_group_id = @investor_group.id
@group_post.post_by = investor_id
if @group_post.message.blank?
flash[:notice] = 'Post can\'t be blank.'
end
if @group_post.save
flash[:notice] = 'Post was successfully created.'
# redirect_to :action => :show, :id => params[:id] - removed after trying to implement ajax via jquery
else
flash[:notice] = 'Post was not successfully created.'
end


respond_to do |format|
format.html {redirect_to :action => "show", :id => params[:id]}
format.js
end

end

最佳答案

我假设你想要这样的东西:

$('#post_msg input').val('');

这将清除消息输入框的值。

关于javascript - 无法通过 Jquery 1.4 用于 ajax 实现的覆盖提交方法清除文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6028413/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com