I have a form that for the most part just submits as a normal form, so I don't want to set in the form_tag the :remote => true option.
However, under certain circumstances I'd like to be able have a javascript function post the form as if it had been posted by :remote => true. What would I need to do in javascript to accomplish this?
In the body tag, created an HTML form and specify the id, method, and action of the form. In the form, specify an anchor tag with an event onclick. Create a function for JavaScript that will get executed when the link is clicked. When we click on the link, the function submitForm() will get executed.
The formmethod attribute defines the HTTP method for sending form-data to the action URL. The formmethod attribute overrides the method attribute of the <form> element. The formmethod attribute is only used for buttons with type="submit".
Hitting enter on text fields can sometimes trigger a form submit. See here. Especially if that is the only element in the form. One way to control the post back is to set the action to empty and fire off the event yourself with Javascript.
The submit event fires when the user clicks a submit button ( <button> or <input type="submit">) or presses Enter while editing a field (e.g. <input type="text">) in a form. The event is not sent to the form when calling the form. submit() method directly.
I'm sorta new to this but here goes...
rails.js (the jquery one at least) defines the following function to catch and submit forms:
$('form').live('submit.rails', function(e) { ... });
If you use the following it should trigger the same function (and if :remote => true, then it wont cause a page reload):
$("#<yourformid>").trigger("submit.rails");
So, if you wanted to submit your form on a select change for example, you could set the above trigger call to the select's :onchange I would imagine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With