When you try to submit a form from Javascript, using form_remote_tag, like this:
Rails:
form_remote_tag :url => {:controller => "somecontroller", :action => "} , :html => {:id => "ajax-form-1">
If you just invoke form.submit(), like this:
var form = document.getElementById('ajax-form-1'); if(form) form.submit();
the form will be submitted to a new page, that’s not soo ajaxy you now think. Instead use:
var form = document.getElementById('ajax-form-1');<br /> if(form.onsubmit()){ form.submit(); }
Now your form will be submitted on the AJAX way!



September 19th, 2007 at 8:07 pm
Great! Thanks!
February 28th, 2009 at 1:14 am
Thanks, it’s of great help!
March 31st, 2009 at 4:32 pm
For JQuery users (with jRails), calling onsubmit() on the form won’t work. Instead, call trigger(’onsubmit’).
May 5th, 2009 at 7:59 am
thanks, by chinese rails coder
August 20th, 2009 at 3:12 pm
Thanks for this info.
I was stumped why my page refreshed even after using form_remote_tag.
Now I know!
September 16th, 2009 at 3:19 pm
Thanks!!! It was of great use to me.