无法通过Jquery 1.4使用用于Ajax实现的重写提交方法来清除文本字段 [英] Unable to clear text field with overridden submit method used for ajax implementation via Jquery 1.4

查看:132
本文介绍了无法通过Jquery 1.4使用用于Ajax实现的重写提交方法来清除文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

<$ p $ ('< div id =new_post><%= escape_javascript(flash.delete(:notice))%>< / div> ;');<! - TO-DO闪光消息尚未启用/正在工作 - >
$(#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)%>前< / 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 | - %GT; 
开始讨论:< label id =post_msg><%= f.text_field:message%>< / label>
<! - <%= f.file_field:photo%> - >
<%= submit_tag发布%>< / p>
<! - < input type =buttonvalue =Reset FormonClick =this.form.reset()/> - >
<%end%>

< div id =latest_post> < / DIV>
<%for a @group_all_posts%>
<%= a.message%>由<%= Investor.find(a.post_by).first_name%> < div class =contentdispgrpid =style_chck> <%= distance_of_time_in_words(a.created_at,Time.now)%>前< / div>< br />< br /> < HR />
<%end%>


< / tr>

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



请帮助我一样。



感谢您的帮助..





我的群组控制器中的post_message代码。

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


除非@ current_user.is_an_existing_member_of_group(@investor_group)
flash [:notice] =请加入此群组参与讨论
redirect_to:action => :show,:id => @investor_group并返回#试图找出这个命令到底做了什么用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] ='发布不能为空'
结束
如果@ group_post.save
flash [:notice] ='发布成功创建'
#redirect_to:action => :show,:id => params [:id] - 试图通过jquery实现ajax后删除
else
flash [:notice] ='发布未成功创建。'
结束


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


<

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

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


I have a functionality of posting messages to a group which works using AJAX via Jquery. When I click on the "Post" button which is the value of the submit_tag, javascript is rendered and the page without reloading displays the latest message posted. Basically the post_message.js.erb file is called. I am making use of the following tutorial to implement the same:- http://railscasts.com/episodes/136-jquery

The code for post_message.js.erb looks like this:-

$("#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();

The part of the form which would otherwise render HTML and display the posted message with reloading the page looks like this:-

<%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>

I want to empty the text field also along with the above already implemented functionality. I guess , I need to some how make use of the Javascript reset method , but somewhere I am not using it correctly.

Kindly help me with the same.

Thanks for your help..

[EDIT]

Code for post_message in my groups controller.

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

解决方案

I assume you want something like this:

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

That will clear the message input box value.

这篇关于无法通过Jquery 1.4使用用于Ajax实现的重写提交方法来清除文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆