Jquery 类似Twitter的内容提交和更新
你好。我现在正在创建一个Quotence服务,在尝试提交和显示类似twitter的数据时遇到了这样的问题:我所有的ajax请求都会执行两次。我使用jQuery的方式如下:Jquery 类似Twitter的内容提交和更新,jquery,ruby-on-rails,ajax,ruby-on-rails-3,form-submit,Jquery,Ruby On Rails,Ajax,Ruby On Rails 3,Form Submit,你好。我现在正在创建一个Quotence服务,在尝试提交和显示类似twitter的数据时遇到了这样的问题:我所有的ajax请求都会执行两次。我使用jQuery的方式如下: 我尝试了.click(),.live(“click”,function())和.one(“click”,function())处理程序和“submit”按钮。他们都执行了两次查询。是的,还有.one(“单击”,function()) 我设置了timeout函数来检查新的引号,它也执行了两次 我的JS/RoR代码有什么问题
- 我尝试了
,.click()
和.live(“click”,function())
处理程序和“submit”按钮。他们都执行了两次查询。是的,还有.one(“click”,function())
.one(“单击”,function())
- 我设置了timeout函数来检查新的引号,它也执行了两次
<div class="msg"></div><br />
<form accept-charset="UTF-8" action="/quotes" class="new_quote" id="new_quote" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /><input name="authenticity_token" type="hidden" value="vD6hAOmZjenRFw1aO1yH75K9K7WTFneJuP3H7sOR/Qw=" /></div>
<div class="field">
<label for="quote_author">Author</label><br />
<input id="quote_author" name="quote[author]" size="30" type="text" />
</div>
<div class="field">
<label for="quote_body">Body</label><br />
<textarea cols="60" id="quote_body" name="quote[body]" rows="8"></textarea>
</div>
<div class="field">
<label for="quote_approved">Approved</label><br />
<input name="quote[approved]" type="hidden" value="0" /><input id="quote_approved" name="quote[approved]" type="checkbox" value="1" />
</div>
<div class="actions">
<input id="quote_submit" name="commit" type="submit" value="Create Quote" />
</div>
</form>
我已经查过你的消息来源了。我认为实现你的目标最好的方法是用Rails的方式。我的意思是您已经使用了jQueryRails脚本。因此,在rails 3应用程序中,您可以轻松地坚持使用UJS和RJS方法: 形式:
form_for(@quote, :remote => true) do |f|
在控制器中:
def create
@quote = Quote.new(params[:quote])
respond_to do |format|
if @quote.save
format.html { redirect_to(@quote, :notice => :quote_created) }
format.xml { render :xml => @quote, :status => :created, :location => @quote }
format.js
else
format.html { render :action => "new" }
format.xml { render :xml => @quote.errors, :status => :unprocessable_entity }
format.js
end
end
end
在create.js.erb中(以下是语法):
您可以使用#data_form
包装表单,使用ul.data_网格将列表括起来,或者在create.js.erb
中更改选择器。此外,在该文件中,您可以清除成功表单,并在此处显示flash消息或任何您需要的内容
毕竟,别忘了从application.js中抛出这段代码(不再需要了):
对不起,我可以通过电子邮件、叽叽喳喳或其他方式与您联系吗?shybovycha gmail com
def create
@quote = Quote.new(params[:quote])
respond_to do |format|
if @quote.save
format.html { redirect_to(@quote, :notice => :quote_created) }
format.xml { render :xml => @quote, :status => :created, :location => @quote }
format.js
else
format.html { render :action => "new" }
format.xml { render :xml => @quote.errors, :status => :unprocessable_entity }
format.js
end
end
end
- if @quote.errors.any?
$('#data_form').html('#{escape_javascript(render(:partial => "form"))}');
- else
$('ul.data_grid').prepend('#{escape_javascript(render :partial => "quote", :locals => { :quote => quote })}');
$("form.new_quote > .actions > [type=submit]").live("click", function() {
$.post('/ajax_new', $('form.new_quote').serialize(), function(resp) {
resp = $.parseJSON(resp);
if (resp[0].done == "ok") {
$(".msg").css("background-color", "#00fe00").text("Ok").fadeIn('slow').delay(5000).fadeOut('slow');
$("#quote_author,#quote_body").each(function(i,e) {
$(this).val("");
});
}
});
return false;
});