jQuery: How to fix this syntax for a live binding
I can't seem to get this right.
I have form coming in via XHR. I need to be able to attach a submit handler to it once it comes in via XHR, hence the .live() here. The code below actually works, the problem is 开发者_如何学JAVAthat an XHR HTTP POST happens (which is good), and then a full page HTTP POST load happens, and that's not good. The return false; seems to be doing nothing. My backend is Rails, but that doesn't mean much in this context. What am I doing wrong in this block of code that's making the HTTP POST happen once for an XHR and once for a full page load? Better way of going about this?
$('form[name=clip_form2]').live('submit', function() {
$.post($(this).attr("action"), $(this).serialize(), null, "script");
return false;
});
My Rails form even has an onsubmit="return false;" handler:
<% form_for(@clip, :url=>group_channel_clip_path(@clip.group_channel,@clip), :method=>:put, :html => {:multipart => true, :name=>'clip_form2', :onsubmit => "return false;"}) do |f| -%>
$(document).ready(function(){
$('form[name=clip_form2]').submit(function(e){
e.preventDefault();
$.post($(this.attr('action')), $(this).serialize(), null, 'script');
});
});
Also this is a really easy to use jquery extension that does this with added features http://jquery.malsup.com/form/
精彩评论